Guest User

Untitled

a guest
Jan 22nd, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class MathTest
  3. {
  4. public static void main (String[] args)
  5. {
  6. Scanner sc=new Scanner(System.in);
  7. String choice,a="";
  8. double x,y,myans,realans=0,diff=0,x1,y1;
  9. final double MAX_DIFF=1e-6;
  10. while(true)
  11. {
  12. System.out.println("-------------------------");
  13. System.out.println("Select mode:");
  14. System.out.println("\'m\' for a manually-set problem.");
  15. System.out.println("\'r\' for a random problem.");
  16. System.out.println("\'q\' to quit the problem.");
  17. System.out.println("-------------------------");
  18. System.out.print(">>");
  19. choice=sc.next();
  20. if(choice.equals("q"))
  21. break;
  22. else if(choice.equals("m"))
  23. {
  24. System.out.println("Enter x >> ");
  25. x=sc.nextDouble();
  26. System.out.println("Enter y >> ");
  27. y=sc.nextDouble();
  28. realans=x/y;
  29. System.out.println("What is the value of "+x+"/"+y+" ?");
  30. myans=sc.nextDouble();
  31. diff=Math.abs(myans-realans);
  32. if(diff<MAX_DIFF)
  33. System.out.println("MathTest says\"Your answer is correct!\"");
  34. else
  35. {
  36. if(myans>realans)
  37. a="high";
  38. else if(myans<realans)
  39. a="low";
  40. System.out.println("Your answer is "+diff+" too "+a);
  41. }
  42. System.out.println("Correct answer is "+realans);
  43. }
  44. else if(choice.equals("r"))
  45. {
  46. x1=Math.random()*100;
  47. y1=Math.random()*100;
  48. x=(double)(Math.round(x1*10))/10;
  49. y=(double)(Math.round(y1*10))/10;
  50. while(y==0)
  51. {
  52. y=(double)(Math.round(y1*10))/10;
  53. }
  54. realans=x/y;
  55. System.out.println("What is the value of "+x+"/"+y+" ?");
  56. myans=sc.nextDouble();
  57. diff=Math.abs(myans-realans);
  58. if(diff<MAX_DIFF)
  59. System.out.println("MathTest says\"Your answer is correct!\"");
  60. else
  61. {
  62. if(myans>realans)
  63. a="high";
  64. else if(myans<realans)
  65. a="low";
  66. System.out.println("Your answer is "+diff+" too "+a);
  67. }
  68. System.out.println("Correct answer is "+realans);
  69. }
  70. System.out.println("-- Press ENTER to take another test --");
  71. }
  72. }
  73.  
  74. }
Add Comment
Please, Sign In to add comment