1c7

james

1c7
Nov 5th, 2019
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. import java.util.Random;
  2. import java.util.Scanner;
  3.  
  4. public class Game {
  5. private static int correct;
  6. private static int i = 0;
  7.  
  8. public static void main(String[] args)
  9. {
  10. boolean playAgain = true;
  11. for (i = 0; i <= 10; i++)
  12. {
  13. Scanner input = new Scanner(System.in);
  14. int firstNum = getRanNum();
  15. int secondNum = getRanNum();
  16. int answer = firstNum + secondNum;
  17. System.out.println("Question: " + (i + 1));
  18. System.out.println("What is the answer to the equation:\n" + firstNum + " + " + secondNum + "?");
  19. System.out.println("Answer:");
  20. int data = input.nextInt();
  21. checkAns(data, answer);
  22. }
  23. System.out.println("You have answered " + correct + " questions correct.");
  24. }
  25.  
  26.  
  27. private static void checkAns(int data, int answer)
  28. {
  29. if (data == answer)
  30. {
  31. correct++;
  32. System.out.println("Correct! " + correct + " answered correctly.\n\n");
  33. } else {
  34. System.out.println("Incorrect! " + correct + " answered correctly.\n\n");
  35. }
  36. }
  37.  
  38. private static int getRanNum()
  39. {
  40. Random rand = new Random();
  41. return rand.nextInt((10 - 1) + 1) + 1;
  42. }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment