Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ru.pangaia.arithmo;
- import java.util.InputMismatchException;
- import java.util.NoSuchElementException;
- import java.util.Random;
- import java.util.Scanner;
- public class MathTest {
- private static Random rnd = new Random();
- public static void main(String[] args) {
- // write your code here
- try {
- int time = Integer.parseInt(args[0]);
- Long timerMillis = new Long(time*60*1000);
- displayGreeting(time);
- Scanner scanner = new Scanner(System.in);
- Long startTime = System.currentTimeMillis();
- int counter = 0;
- int rightAnswers = 0;
- while (System.currentTimeMillis()-startTime < timerMillis)
- {
- counter++;
- try {
- if(showMiniTask() == scanner.nextInt())
- {
- rightAnswers++;
- }
- }
- catch(InputMismatchException ex) {break;}
- catch(NoSuchElementException ex) {break;}
- }
- displayStats(time,counter,rightAnswers);
- }
- catch (NumberFormatException ex) {
- System.exit(1);
- }
- }
- public static void displayGreeting(int time) {
- System.out.printf("Please enter answers for math questions\nyou have %d minutes\n",time);
- System.out.println("===========Start!==========");
- }
- public static void displayStats(int time,int tot,int right) {
- System.out.printf("\ntimer for %d minutes, correct/total: %d/%d\n", time, right, tot);
- }
- public static int showMiniTask() {
- int a = rnd.nextInt(10);
- int b = rnd.nextInt(10);
- int c = rnd.nextInt(10);
- int pm1 = rnd.nextInt(2);
- int pm2 = rnd.nextInt(2);
- System.out.printf("%d %s %d %s %d = ",a,pm1==0?"+":"-",b,pm2==0?"+":"-",c);
- int result = 0;
- result +=a;
- if (pm1==0) {
- result+=b;
- }
- else {
- result-=b;
- }
- if (pm2==0){
- result+=c;
- }
- else {
- result-=c;
- }
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement