Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. package ru.pangaia.arithmo;
  2.  
  3. import java.util.InputMismatchException;
  4. import java.util.NoSuchElementException;
  5. import java.util.Random;
  6. import java.util.Scanner;
  7.  
  8. public class MathTest {
  9.  
  10. private static Random rnd = new Random();
  11.  
  12. public static void main(String[] args) {
  13. // write your code here
  14. try {
  15. int time = Integer.parseInt(args[0]);
  16. Long timerMillis = new Long(time*60*1000);
  17. displayGreeting(time);
  18. Scanner scanner = new Scanner(System.in);
  19. Long startTime = System.currentTimeMillis();
  20. int counter = 0;
  21. int rightAnswers = 0;
  22. while (System.currentTimeMillis()-startTime < timerMillis)
  23. {
  24. counter++;
  25.  
  26. try {
  27. if(showMiniTask() == scanner.nextInt())
  28. {
  29. rightAnswers++;
  30. }
  31. }
  32. catch(InputMismatchException ex) {break;}
  33. catch(NoSuchElementException ex) {break;}
  34. }
  35. displayStats(time,counter,rightAnswers);
  36. }
  37. catch (NumberFormatException ex) {
  38. System.exit(1);
  39. }
  40.  
  41. }
  42. public static void displayGreeting(int time) {
  43. System.out.printf("Please enter answers for math questions\nyou have %d minutes\n",time);
  44. System.out.println("===========Start!==========");
  45.  
  46. }
  47. public static void displayStats(int time,int tot,int right) {
  48. System.out.printf("\ntimer for %d minutes, correct/total: %d/%d\n", time, right, tot);
  49. }
  50. public static int showMiniTask() {
  51. int a = rnd.nextInt(10);
  52. int b = rnd.nextInt(10);
  53. int c = rnd.nextInt(10);
  54. int pm1 = rnd.nextInt(2);
  55. int pm2 = rnd.nextInt(2);
  56. System.out.printf("%d %s %d %s %d = ",a,pm1==0?"+":"-",b,pm2==0?"+":"-",c);
  57. int result = 0;
  58. result +=a;
  59. if (pm1==0) {
  60. result+=b;
  61. }
  62. else {
  63. result-=b;
  64. }
  65. if (pm2==0){
  66. result+=c;
  67. }
  68. else {
  69. result-=c;
  70. }
  71. return result;
  72. }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement