I have a school project assignment and the assignment is to create a math "game" that prompts the user for their name, asks them if they want to do addition, subtraction, multiplication or division. Once they select, it picks 2 random integers between 0-10. If they get it correct, it should congratulate them, ask if they want to play again. If they get it wrong, they get 3 chances. At the end of the game when they don't want to play anymore, it is supposed to give them the total problems they did, # right, # wrong, and percentage of them getting it right. We are just learning loops and I'm probably doing something stupid for it not to work. Copying and pasting the code. Apologize for all of the comments, teacher requires them.
import java.util.Scanner; import java.util.Random; public class MathGameJMB { public static void main(String[] args) { String name; // hold value for user's name int number1; // hold value of 1st random number int number2; // hold value of 2nd random number int choice; // hold value of menu choice chosen by user int guess; // hold value for user's guess to math problem int remainder; // hold value for remainder for division problem String again = "y"; // hold value for y/n value if user wants to keep playing int attempt = 1; // hold value for the attempt number that the user is on int answer = 0; // hold value for the answer to the math problem int remainderGuess; // holds value for user's guess of the remainder int gamesPlayed = 0; // Number of games the user has played int answersCorrect = 0; // hold value for number of answers the user gets correct int answersWrong = 0; //hold value for number of answers the user gets wrong double percentage; // Percentage of answers the user got correct // Create a Scanner object for keyboard input. Scanner keyboard = new Scanner(System.in); // Create a Random object to generate random numbers. Random rand = new Random(); // Prompt the user to enter their name, then read the input. System.out.print("Please enter your name: "); name = keyboard.next(); // Echo print the user's name System.out.println("Hello " + name + "!" + " Let's play a simple Math game."); while (again.equalsIgnoreCase("y")) { System.out.println("1. Addition"); System.out.println("2. Subtraction"); System.out.println("3. Multiplication"); System.out.println("4. Division"); System.out.print("What type of problem would you like to solve? Enter your selection: "); choice = keyboard.nextInt(); System.out.println("Okay, let's play! "); // Get two random integers between 0 and 10. number1 = rand.nextInt(10); number2 = rand.nextInt(9); switch (choice) { case 1: System.out.println("The problem is " + number1 + " + " + number2 + "."); answer = number1 + number2; break; case 2: System.out.println("The problem is " + number1 + " - " + number2 + "."); answer = number1 - number2; break; case 3: System.out.println("The problem is " + number1 + " - " + number2 + "."); answer = number1 * number2; break; case 4: System.out.println("The problem is " + number1 + " - " + number2 + "."); answer = number1 / number2; default: System.out.println("Invalid Choice"); } while (attempt <= 3) { System.out.print("What is your answer? "); guess = keyboard.nextInt(); if (choice == 4) { System.out.print("Enter the remainder: "); remainder = number1 % number2; remainderGuess = keyboard.nextInt(); if (guess == answer) { if (choice == 4 && remainder == remainderGuess) { System.out.println("Congratulations! You answered correctly!"); } System.out.println("Congratulations! You answered correctly!"); answersCorrect++; gamesPlayed++; } else { System.out.println("Sorry, that answer is incorrect. Please try again."); attempt++; } } while (attempt >3) { gamesPlayed++; System.out.println("Study a bit more and then please try the game again. Practice makes perfect!"); } System.out.print("Would you like to play again? (Enter Y to continue and N to exit): "); again = again = keyboard.next(); } } while (again.equalsIgnoreCase("n")) { System.out.println("You played the game " + gamesPlayed + " time(s)."); System.out.println("You were correct " + answersCorrect + " time(s)."); System.out.println("You were incorrect " + answersWrong + " time(s)."); percentage = (double)answersCorrect / gamesPlayed; System.out.println("You got the answer correct " + percentage + "% " + "of the time."); System.out.println("Good bye " + name + " ! I hope you'll play again soon!"); } } }