Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.24 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package hoa2mathapp_dela.cruz;
  7. import java.util.Scanner;
  8. import java.util.Random;
  9. /**
  10. *
  11. * @author Grade 10
  12. */
  13. public class HOA2MathApp_DelaCruz {
  14.  
  15. /**
  16. * @param args the command line arguments
  17. */
  18. public static void main(String[] args) {
  19.  
  20. int v = 0;
  21. Scanner zz = new Scanner(System.in);
  22. Random zed= new Random();
  23. String retry = "YES";
  24. while (v==0){ //in order to loop infinitely until the user decides to end the program
  25. if(retry.equals("YES") || retry.equals("Y")){ //checker if it should loop
  26. int z = 0;
  27. int ta;
  28. int in;
  29. System.out.println("~ WELCOME TO THE MATH CHALLENGE ~");
  30. System.out.println("TO begin, please select an operator: ");
  31. System.out.println("||+||-||*||/||");
  32. char operator = zz.next().charAt(0); //to get the user's chosen operator
  33. switch (operator){ //switch case for varying operators
  34. case '+':
  35. System.out.println("You have selected Addition!");
  36. while (z < 5){ // counter & limit
  37. int r1 = zed.nextInt(15); //to get a random number from 1-50 for r1
  38. int r2 = zed.nextInt(15); //to get a random number from 1-50 for r2
  39. System.out.println(r1 + " + " + r2);
  40. ta = r1 + r2; //computes the correct answer
  41. in = zz.nextInt();
  42. if (in == ta){ // verifies if user's answer is correct or not
  43. System.out.println("You are Correct!");
  44. z++; //counter & limit
  45. }
  46. else {
  47. System.out.println("That is the Wrong Answer!");
  48. System.out.println(" The Answer is " + ta);
  49. }
  50. }
  51. break;
  52. // same rules of codes are applied below
  53. case '-':
  54. System.out.println("You have chosen Subtraction!");
  55. while (z < 5){
  56. int r1 = zed.nextInt(15);
  57. int r2 = zed.nextInt(15);
  58. System.out.println(r1 + " - " + r2);
  59. ta = r1 - r2;
  60. in = zz.nextInt();
  61. if (in == ta){
  62. System.out.println("You are Correct!");
  63. z++;
  64. }
  65. else {
  66. System.out.println("That is the Wrong Answer!");
  67. System.out.println(" The Answer is " + ta);
  68. }
  69. }
  70. break;
  71.  
  72. case '*':
  73. System.out.println("You have selected Multiplication!");
  74. while (z < 5){
  75. int r1 = zed.nextInt(15);
  76. int r2 = zed.nextInt(15);
  77. System.out.println(r1 + " * " + r2);
  78. ta = r1 * r2;
  79. in = zz.nextInt();
  80. if (in == ta){
  81. System.out.println("Correct!");
  82. z++;
  83. }
  84. else {
  85. System.out.println("That is the Wrong Answer!");
  86. System.out.println(" The Answer is " + ta);
  87. }
  88. }
  89. break;
  90.  
  91. case '/':
  92. System.out.println("You have chosen Division!");
  93. while (z < 5){
  94. int r1 = zed.nextInt(15);
  95. int r2 = zed.nextInt(15);
  96. System.out.println(r1 + " / " + r2);
  97. ta = r1 / r2;
  98. in = zz.nextInt();
  99. if (in == ta){
  100. System.out.println("Correct!");
  101. z++;
  102. }
  103. else {
  104. System.out.println("Wrong Answer!");
  105. System.out.println(" The Answer is " + ta);
  106. }
  107. }
  108. break;
  109.  
  110. default :
  111. System.out.println("\nPlease input a valid operator!");
  112. }
  113.  
  114. System.out.println("Would you like to try again?");
  115. System.out.println("YES or NO");
  116. retry = zz.next().toUpperCase(); //so that the user doesn't have to worry on monitoring wether it's uppercase or not
  117.  
  118. }
  119. else {
  120. zz.close(); //closes scanner
  121. break; //breaks loop
  122. }
  123. }
  124. System.out.println("Thank you come again!");
  125.  
  126. }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement