Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- import java.util.Scanner;
- public class Game {
- private static int correct;
- private static int i = 0;
- public static void main(String[] args)
- {
- boolean playAgain = true;
- for (i = 0; i <= 10; i++)
- {
- Scanner input = new Scanner(System.in);
- int firstNum = getRanNum();
- int secondNum = getRanNum();
- int answer = firstNum + secondNum;
- System.out.println("Question: " + (i + 1));
- System.out.println("What is the answer to the equation:\n" + firstNum + " + " + secondNum + "?");
- System.out.println("Answer:");
- int data = input.nextInt();
- checkAns(data, answer);
- }
- System.out.println("You have answered " + correct + " questions correct.");
- }
- private static void checkAns(int data, int answer)
- {
- if (data == answer)
- {
- correct++;
- System.out.println("Correct! " + correct + " answered correctly.\n\n");
- } else {
- System.out.println("Incorrect! " + correct + " answered correctly.\n\n");
- }
- }
- private static int getRanNum()
- {
- Random rand = new Random();
- return rand.nextInt((10 - 1) + 1) + 1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment