Results 1 to 14 of 14
  1. #1
    mattmattmatt is offline Member
    Join Date
    Oct 2013
    Posts
    7
    Rep Power
    0

    Default Trying to add variable for remainder into my code

    I am taking java this semester in school, and I have a project assignment to design a math game for the user to calculate addition/subtraction/multiplication/division problem. The user picks what type of problem they want to solve, and the program generates to random integers between 0-10 to use and the user solves. I've gotten everything that I needed into the program, EXCEPT for the remainder. I'm not sure exactly where to put that in, and ask for it when it's required to see if the user got the problem correct without messing the rest of my code up. Any advice is greatly appreciated.


    import java.util.Scanner;
    import java.util.Random;

    public class MathGameJMB
    {
    public static void main(String[] args)
    {
    // Create a Scanner object for keyboard input.
    Scanner keyboard = new Scanner(System.in);

    // Create random object to generate integers
    Random rand = new Random();

    // Prompt the user to enter their name, then read the input.
    System.out.print("Please enter your name: ");
    String userName = keyboard.next();

    // Echo print the user's name
    System.out.println("Hello " + userName + "!" + " Let's play a simple Math game.");

    String playAgain = "y"; // initializes variable for the user playing the game
    int gamesPlayed = 0; //initialize value for number of games the user has played
    int gamesWon = 0; // initialize the value for the number of games the user has won
    int answer = 0; // initialize variable for the calculated answer to the math problem
    int remainder; // initialize variable for the remainder of the division problem for the user

    do {
    System.out.println("1. Addition");
    System.out.println("2. Subtraction");
    System.out.println("3. Multiplication");
    System.out.println("4. Division");

    System.out.print("From the choices above, what type of problem would you like to solve? Enter your selection: ");
    int choice = keyboard.nextInt(); // initializes & reads the input for the user's choice of type of math problem

    System.out.println("Okay, let's play! ");

    // Get two random integers between 0 and 10.
    int number1 = rand.nextInt(10) + 1;
    int number2 = rand.nextInt(10) + 1;


    if (choice == 1) {
    System.out.println("The problem is " + number1 + " + " + number2 + ".");
    answer = number1 + number2;
    } else if (choice == 2) {
    System.out.println("The problem is " + number1 + " - " + number2 + ".");
    answer = number1 - number2;
    } else if (choice == 3) {
    System.out.println("The problem is " + number1 + " * " + number2 + ".");
    answer = number1 * number2;
    } else if (choice == 4) {
    System.out.println("The problem is " + number1 + " / " + number2 + ".");
    answer = number1 / number2;
    }

    final int MAX_TRIES = 3; // Initialize the final value for maxium numnber of tries the user gets
    int tries = 1; // initializing variable for number of tries the user currently has

    System.out.print("What is your answer? ");
    int guess = keyboard.nextInt(); // initialize the variable for the user's guess

    while (tries < MAX_TRIES && guess != answer) {
    tries++;
    System.out.println("Sorry, that answer is incorrect. Please try again.");
    System.out.print("What is your answer? ");
    guess = keyboard.nextInt(); // initialize the variable for the user's guess
    }

    if (guess == answer) {
    System.out.println("Congratulations! You answered correctly!");
    gamesWon++;
    gamesPlayed++;

    } else {
    System.out.println("Study a bit more and then please try the game again. Practice makes perfect!");
    gamesPlayed++;
    }



    System.out.print("Would you like to play again? (Enter Y to continue and N to exit): ");
    playAgain = keyboard.next();

    } while (playAgain.equals("y"));

    //if (playAgain.equals("n")); {
    System.out.println("You played the game " + gamesPlayed + " time(s).");
    System.out.println("You were correct " + gamesWon + " time(s).");
    System.out.println("You were incorrect " + (gamesPlayed - gamesWon) + " time(s).");

    double percentage = (double)gamesWon / gamesPlayed * 100; // initialize the variable for the user's winning percentage at the game
    System.out.println("You got the answer correct " + percentage + "% " + "of the time.");
    System.out.println("Good bye " + userName + " ! I hope you'll play again soon!");
    }
    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Trying to add variable for remainder into my code

    Since a remainder is only part of division then I suggest you handle it in the code where you do division.

  3. #3
    mattmattmatt is offline Member
    Join Date
    Oct 2013
    Posts
    7
    Rep Power
    0

    Default Re: Trying to add variable for remainder into my code

    Any tips on exactly how to do that? Since I have to ask for the answer also? Like you said, the remainder is only with division so I'm lost on how to put that in there and where.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Trying to add variable for remainder into my code

    Where do you calculate the division answer?
    Where do you think you should calculate the remainder?

    When you ask the user for their answer you also need to ask the user for their remainder but only for division. How do you think you can do something only some of the time?

  5. #5
    mattmattmatt is offline Member
    Join Date
    Oct 2013
    Posts
    7
    Rep Power
    0

    Default Re: Trying to add variable for remainder into my code

    I took a shot and put it in there. This is what I got now. Now the issue is when I get a division problem, it will ask for the answer, but never prompt for the remainder.


    import java.util.Scanner;
    import java.util.Random;

    public class MathGameJMB
    {
    public static void main(String[] args)
    {
    // Create a Scanner object for keyboard input.
    Scanner keyboard = new Scanner(System.in);

    // Create random object to generate integers
    Random rand = new Random();

    // Prompt the user to enter their name, then read the input.
    System.out.print("Please enter your name: ");
    String userName = keyboard.next();

    // Echo print the user's name
    System.out.println("Hello " + userName + "!" + " Let's play a simple Math game.");

    String playAgain = "y"; // initializes variable for the user playing the game
    int gamesPlayed = 0; //initialize value for number of games the user has played
    int gamesWon = 0; // initialize the value for the number of games the user has won
    int answer = 0; // initialize variable for the calculated answer to the math problem
    int remainder = 0; // initialize variable for the remainder of the division problem for the user

    do {
    System.out.println("1. Addition");
    System.out.println("2. Subtraction");
    System.out.println("3. Multiplication");
    System.out.println("4. Division");

    System.out.print("From the choices above, what type of problem would you like to solve? Enter your selection: ");
    int choice = keyboard.nextInt(); // initializes & reads the input for the user's choice of type of math problem

    System.out.println("Okay, let's play! ");

    // Get two random integers between 0 and 10.
    int number1 = rand.nextInt(10) + 1;
    int number2 = rand.nextInt(10) + 1;


    if (choice == 1)
    {
    System.out.println("The problem is " + number1 + " + " + number2 + ".");
    answer = number1 + number2;
    }
    else if (choice == 2)
    {
    System.out.println("The problem is " + number1 + " - " + number2 + ".");
    answer = number1 - number2;
    }
    else if (choice == 3)
    {
    System.out.println("The problem is " + number1 + " * " + number2 + ".");
    answer = number1 * number2;
    }
    else if (choice == 4)
    {
    System.out.println("The problem is " + number1 + " / " + number2 + ".");
    answer = number1 / number2;
    choice = number1 % number2;
    }

    final int MAX_TRIES = 3; // Initialize the final value for maxium numnber of tries the user gets
    int tries = 1; // initializing variable for number of tries the user currently has

    System.out.print("What is your answer? ");
    int guess = keyboard.nextInt(); // initialize the variable for the user's guess
    int remainderGuess = 0; // initialize the value and variable for the user's guess at what the remainder is

    if (choice == 4)
    {
    System.out.print("Enter the remainder: ");
    remainderGuess = keyboard.nextInt();
    }

    while (tries < MAX_TRIES && guess != answer || remainderGuess != remainder) {
    tries++;
    System.out.println("Sorry, that answer is incorrect. Please try again.");
    System.out.print("What is your answer? ");
    guess = keyboard.nextInt(); // record the input for the user's guess

    if (choice == 4)
    {
    System.out.print("Enter the remainder: ");
    remainderGuess = keyboard.nextInt();
    }
    }

    if (guess == answer)
    {
    if (choice == 4 && remainderGuess == remainder)
    {
    System.out.println("Congratulations! You answered correctly!");
    }

    System.out.println("Congratulations! You answered correctly!");
    gamesWon++;
    gamesPlayed++;
    }
    else
    {
    System.out.println("Study a bit more and then please try the game again. Practice makes perfect!");
    gamesPlayed++;
    }



    System.out.print("Would you like to play again? (Enter Y to continue and N to exit): ");
    playAgain = keyboard.next();

    } while (playAgain.equals("y"));

    // Once the user has chosen not to play the game again, output how many games they have played, times won, times lost, and winning percentage.
    System.out.println("You played the game " + gamesPlayed + " time(s).");
    System.out.println("You were correct " + gamesWon + " time(s).");
    System.out.println("You were incorrect " + (gamesPlayed - gamesWon) + " time(s).");

    double percentage = (double)gamesWon / gamesPlayed * 100; // initialize the variable for the user's winning percentage at the game
    System.out.println("You got the answer correct " + percentage + "% " + "of the time.");
    System.out.println("Good bye " + userName + " ! I hope you'll play again soon!");

    if (percentage >= 90)
    {
    System.out.println("You have excellent math skills!");
    }
    else if (percentage >= 80 && percentage < 90)
    {
    System.out.println("You are doing a great job, but there is room for improvement!");
    }
    else if (percentage < 80 && percentage >= 70)
    {
    System.out.println("You have average math skills.");
    }
    else if (percentage < 70)
    {
    System.out.println("You need more practice in order to improve your math skills.");
    }
    }
    }

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Trying to add variable for remainder into my code

    Java Code:
    else if (choice == 4)
    {
    System.out.println("The problem is " + number1 + " / " + number2 + ".");
    answer = number1 / number2;
    choice = number1 % number2;
    The choice variable is used to hold what math operation the user wants to do. Why are you assigning the remainder to it?

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Trying to add variable for remainder into my code


  8. #8
    mattmattmatt is offline Member
    Join Date
    Oct 2013
    Posts
    7
    Rep Power
    0

    Default Re: Trying to add variable for remainder into my code

    Yay!! Thank you!! I was testing the program going through scenarios so I picked addition first and guessed incorrectly on 1st attempt and correctly on 2nd attempt, but still told me it wrong incorrect.

    New code:

    import java.util.Scanner;
    import java.util.Random;

    public class MathGameJMB
    {
    public static void main(String[] args)
    {
    // Create a Scanner object for keyboard input.
    Scanner keyboard = new Scanner(System.in);

    // Create random object to generate integers
    Random rand = new Random();

    // Prompt the user to enter their name, then read the input.
    System.out.print("Please enter your name: ");
    String userName = keyboard.next();

    // Echo print the user's name
    System.out.println("Hello " + userName + "!" + " Let's play a simple Math game.");

    String playAgain = "y"; // initializes variable for the user playing the game
    int gamesPlayed = 0; //initialize value for number of games the user has played
    int gamesWon = 0; // initialize the value for the number of games the user has won
    int answer = 0; // initialize variable for the calculated answer to the math problem
    int remainder = 0; // initialize variable for the remainder of the division problem for the user

    do {
    System.out.println("1. Addition");
    System.out.println("2. Subtraction");
    System.out.println("3. Multiplication");
    System.out.println("4. Division");

    System.out.print("From the choices above, what type of problem would you like to solve? Enter your selection: ");
    int choice = keyboard.nextInt(); // initializes & reads the input for the user's choice of type of math problem

    System.out.println("Okay, let's play! ");

    // Get two random integers between 0 and 10.
    int number1 = rand.nextInt(10) + 1;
    int number2 = rand.nextInt(10) + 1;


    if (choice == 1)
    {
    System.out.println("The problem is " + number1 + " + " + number2 + ".");
    answer = number1 + number2;
    }
    else if (choice == 2)
    {
    System.out.println("The problem is " + number1 + " - " + number2 + ".");
    answer = number1 - number2;
    }
    else if (choice == 3)
    {
    System.out.println("The problem is " + number1 + " * " + number2 + ".");
    answer = number1 * number2;
    }
    else if (choice == 4)
    {
    System.out.println("The problem is " + number1 + " / " + number2 + ".");
    answer = number1 / number2;
    remainder = number1 % number2;
    }

    final int MAX_TRIES = 3; // Initialize the final value for maxium numnber of tries the user gets
    int tries = 1; // initializing variable for number of tries the user currently has

    System.out.print("What is your answer? ");
    int guess = keyboard.nextInt(); // initialize the variable for the user's guess
    int remainderGuess = 0; // initialize the value and variable for the user's guess at what the remainder is

    if (choice == 4)
    {
    System.out.print("Enter the remainder: ");
    remainderGuess = keyboard.nextInt();
    }

    while (tries < MAX_TRIES && guess != answer || remainderGuess != remainder) {
    tries++;
    System.out.println("You answered " + guess + "." + " Sorry, that answer is incorrect. Please try again.");
    System.out.print("What is your answer? ");
    guess = keyboard.nextInt(); // record the input for the user's guess

    if (choice == 4)
    {
    System.out.print("Enter the remainder: ");
    remainderGuess = keyboard.nextInt();
    }
    }

    if (guess == answer)
    {
    if (choice == 4 && remainderGuess == remainder)
    {
    System.out.println("Your answer is " + guess + " with a remainder of " + remainderGuess + ".");
    System.out.println("Congratulations! You answered correctly!");
    }
    else if (choice != 4)
    {
    System.out.println("Your answer is " + guess + ".");
    System.out.println("Congratulations! You answered correctly!");
    }
    gamesWon++;
    gamesPlayed++;
    }
    else
    {
    System.out.println("Study a bit more and then please try the game again. Practice makes perfect!");
    gamesPlayed++;
    }



    System.out.print("Would you like to play again? (Enter Y to continue and N to exit): ");
    playAgain = keyboard.next();

    } while (playAgain.equals("y")); // end of loop. the loop will execute as long as the variable playAgain is equal to y.

    // Once the user has chosen not to play the game again, output how many games they have played, times won, times lost, and winning percentage.
    System.out.println("You played the game " + gamesPlayed + " time(s).");
    System.out.println("You were correct " + gamesWon + " time(s).");
    System.out.println("You were incorrect " + (gamesPlayed - gamesWon) + " time(s).");

    double percentage = (double)gamesWon / gamesPlayed * 100; // initialize the variable for the user's winning percentage at the game
    System.out.println("You got the answer correct " + percentage + "% " + "of the time.");

    // output message to the user based on their the percentage of problems they answered correctly
    if (percentage >= 90)
    {
    System.out.println("You have excellent math skills!");
    }
    else if (percentage >= 80 && percentage < 90)
    {
    System.out.println("You are doing a great job, but there is room for improvement!");
    }
    else if (percentage < 80 && percentage >= 70)
    {
    System.out.println("You have average math skills.");
    }
    else if (percentage < 70)
    {
    System.out.println("You need more practice in order to improve your math skills.");
    }

    System.out.println("Good bye " + userName + " ! I hope you'll play again soon!"); // Output message to the user telling them good bye

    }
    }

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Trying to add variable for remainder into my code

    Do you have a question or have you managed to complete the program?

  10. #10
    mattmattmatt is offline Member
    Join Date
    Oct 2013
    Posts
    7
    Rep Power
    0

    Default Re: Trying to add variable for remainder into my code

    Yeah the question would be what is causing it to say the answer is incorrect on the 2nd attempt even though I answer it correctly. I basically answered it wrong on purpose on the 1st attempt to test it. When the 2nd attempt came up, I put in the right answer and it still said it was wrong.

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Trying to add variable for remainder into my code

    Please repost your code with code tags. Place [ code ] before and [ /code ] after your code (without the spaces).

  12. #12
    mattmattmatt is offline Member
    Join Date
    Oct 2013
    Posts
    7
    Rep Power
    0

    Default Re: Trying to add variable for remainder into my code

    Java Code:
    import java.util.Scanner;
    import java.util.Random;
    
    public class MathGameJMB
    {
    	public static void main(String[] args)
    	{
    		// Create a Scanner object for keyboard input.
    		Scanner keyboard = new Scanner(System.in);
    		
    		// Create random object to generate integers
    		Random rand = new Random(); 
    		
    		// Prompt the user to enter their name, then read the input.
    		System.out.print("Please enter your name: ");
    		String userName = keyboard.next();
    		
    		// Echo print the user's name
    		System.out.println("Hello " + userName + "!" + " Let's play a simple Math game.");
    		
    		String playAgain = "y";  // initializes variable for the user playing the game
    		int gamesPlayed = 0;  //initialize value for number of games the user has played
    		int gamesWon = 0;  // initialize the value for the number of games the user has won
    		int answer = 0;  // initialize variable for the calculated answer to the math problem
    		int remainder = 0;  // initialize variable for the remainder of the division problem for the user
    		
    		do {
    			System.out.println("1. Addition");
    			System.out.println("2. Subtraction");
    			System.out.println("3. Multiplication");
    			System.out.println("4. Division");
    			
    			System.out.print("From the choices above, what type of problem would you like to solve? Enter your selection: ");
    			int choice = keyboard.nextInt();  // initializes & reads the input for the user's choice of type of math problem
    			
    			System.out.println("Okay, let's play! ");
    			
    			// Get two random integers between 0 and 10. 
    			int number1 = rand.nextInt(10) + 1;
    			int number2 = rand.nextInt(10) + 1;
    			
    						
    			if (choice == 1) 
    			{
    				System.out.println("The problem is " + number1 + " + " + number2 + ".");
    				answer = number1 + number2; 
    			} 
    			else if (choice == 2) 
    			{
    				System.out.println("The problem is " + number1 + " - " + number2 + ".");
    				answer = number1 - number2;
    			} 
    			else if (choice == 3) 
    			{
    				System.out.println("The problem is " + number1 + " * " + number2 + ".");
    				answer = number1 * number2;
    			} 
    			else if (choice == 4) 
    			{
    				System.out.println("The problem is " + number1 + " / " + number2 + ".");
    				answer = number1 / number2;
    				remainder = number1 % number2;
    			}
    			
    			final int MAX_TRIES = 3; // Initialize the final value for maxium numnber of tries the user gets
    			int tries = 1; // initializing variable for number of tries the user currently has
    						
    			System.out.print("What is your answer? ");
    			int guess = keyboard.nextInt();  // initialize the variable for the user's guess
    			int remainderGuess = 0;  // initialize the value and variable for the user's guess at what the remainder is
    			
    			if (choice == 4)
    			{
    				System.out.print("Enter the remainder: ");
    				remainderGuess = keyboard.nextInt();
    			}
    			
    			while (tries < MAX_TRIES && guess != answer || remainderGuess != remainder) {
    				tries++;
    				System.out.println("You answered " + guess + "." + " Sorry, that answer is incorrect. Please try again.");
    				System.out.print("What is your answer? ");
    				guess = keyboard.nextInt();  // record the input for the user's guess
    				
    				if (choice == 4)
    				{
    					System.out.print("Enter the remainder: ");
    					remainderGuess = keyboard.nextInt();
    				}
    			}
    			
    			if (guess == answer)
    			{
    				if (choice == 4 && remainderGuess == remainder)
    				{
    					System.out.println("Your answer is " + guess + " with a remainder of " + remainderGuess + ".");
    					System.out.println("Congratulations! You answered correctly!");
    				}
    				else if (choice != 4)
    				{
    				System.out.println("Your answer is " + guess + ".");	
    				System.out.println("Congratulations! You answered correctly!");
    				}
    				gamesWon++;
    				gamesPlayed++;
    			} 
    			else
    			{
    				System.out.println("Study a bit more and then please try the game again. Practice makes perfect!");
    				gamesPlayed++;
    			}
    			
    				
    				
    			System.out.print("Would you like to play again? (Enter Y to continue and N to exit): ");
    			playAgain = keyboard.next();
    			
    		} while (playAgain.equals("y"));  // end of loop. the loop will execute as long as the variable playAgain is equal to y.
    		
    		// Once the user has chosen not to play the game again, output how many games they have played, times won, times lost, and winning percentage.
    		System.out.println("You played the game " + gamesPlayed + " time(s).");
    		System.out.println("You were correct " + gamesWon + " time(s).");
    		System.out.println("You were incorrect " + (gamesPlayed - gamesWon) + " time(s).");
    		
    		double percentage = (double)gamesWon / gamesPlayed * 100;  // initialize the variable for the user's winning percentage at the game
    		System.out.println("You got the answer correct " + percentage + "% " + "of the time.");
    		
    		// output message to the user based on their the percentage of problems they answered correctly
    		if (percentage >= 90)
    		{
    			System.out.println("You have excellent math skills!");
    		}
    		else if (percentage >= 80 && percentage < 90)
    		{
    			System.out.println("You are doing a great job, but there is room for improvement!");
    		}
    		else if (percentage < 80 && percentage >= 70)
    		{
    			System.out.println("You have average math skills.");
    		}
    		else if (percentage < 70)
    		{
    			System.out.println("You need more practice in order to improve your math skills.");
    		}
    		
    		System.out.println("Good bye " + userName + " ! I hope you'll play again soon!");  // Output message to the user telling them good bye
    
    	}
    }

  13. #13
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Trying to add variable for remainder into my code

    Does the problem occur for addition, division, all operations etc?
    You have three conditions in the while statement. Try printing all three out to see what results they give and make sure they are what you expect. For example:
    Java Code:
    while (tries < MAX_TRIES && guess != answer || remainderGuess != remainder) {
        System.out.println(tries < MAX_TRIES);
        ....

  14. #14
    mattmattmatt is offline Member
    Join Date
    Oct 2013
    Posts
    7
    Rep Power
    0

    Default Re: Trying to add variable for remainder into my code

    Thank you for all of your help. I really appreciate it. I was able to print a copy of it out and my professor gave me just enough info to get it corrected. I just had to reset the remainder value to zero. I was doing that, but it was before the loop, so it wasn't resetting with every game.

Similar Threads

  1. Remainder - Coins
    By Choustonator in forum New To Java
    Replies: 1
    Last Post: 11-13-2012, 04:15 PM
  2. I need help finding a remainder
    By MrPosotive in forum New To Java
    Replies: 2
    Last Post: 09-24-2012, 06:01 AM
  3. How to calculate remainder in JAVA (%)
    By batista11b5 in forum New To Java
    Replies: 25
    Last Post: 10-25-2010, 07:39 AM
  4. how to discard remainder on division?
    By RobertF in forum New To Java
    Replies: 9
    Last Post: 03-13-2009, 12:20 PM
  5. BigInteger remainder results in zero
    By perito in forum New To Java
    Replies: 1
    Last Post: 03-21-2008, 04:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •