Results 1 to 15 of 15
  1. #1
    gabrielpr12 is offline Member
    Join Date
    Nov 2011
    Posts
    41
    Rep Power
    0

    Default Number Guessing Game issue

    I am trying to write a code that asks the user to enter a number between 1-1000. The program will keep asking the user to continue guessing until he gets it right, telling him if the guess is too high or too low. When the user guess the right number a message will display that he guessed the number, how many tries it took him and if he/she would like to play again. So far I have the code that keeps asking the user for a number, but I don't know how to incorporate another loop to repeat the first loop if the user wants to play again. Where in the code do I insert the second loop and do I use a another do..while loop or use for or if loops? Here is my code:

    Java Code:
    import java.util.Scanner;
     
    public class RandomGuess3 {
          public static void main(String[] args) {
                int secretNumber;
                secretNumber = (int) (Math.random() * 999 + 1);           
                Scanner keyboard = new Scanner(System.in);
                int guess;
    				String answer;
                do {
                      System.out.print("Enter a guess (1-1000): ");
                      guess = keyboard.nextInt();
                      if (guess == secretNumber)
                            System.out.println("Your guess is correct. Congratulations!");
                      else if (guess < secretNumber)
                            System.out.println("Your guess is smaller than the secret number.");
                      else if (guess > secretNumber)
                            System.out.println("Your guess is greater than the secret number.");
                	} while (guess != secretNumber);
          }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Number Guessing Game issue

    Quote Originally Posted by gabrielpr12 View Post
    ...So far I have the code that keeps asking the user for a number, ...
    You'll need to add an int counter variable that you increase inside of the while loop.

    ...but I don't know how to incorporate another loop to repeat the first loop if the user wants to play again. Where in the code do I insert the second loop and do I use a another do..while loop or use for or if loops?
    Experiment with this -- you'll probably get it right, if not the first time, then soon. If you fail after several tries, show us your attempt.

  3. #3
    gabrielpr12 is offline Member
    Join Date
    Nov 2011
    Posts
    41
    Rep Power
    0

    Default Re: Number Guessing Game issue

    Quote Originally Posted by Fubarable View Post
    You'll need to add an int counter variable that you increase inside of the while loop.



    Experiment with this -- you'll probably get it right, if not the first time, then soon. If you fail after several tries, show us your attempt.
    But, if I add a counter I would have to create a for loop or something to that effect, my question is where do I put the second loop? I have to ask the user if they want to play again, and if the answer is yes, then I have to repeat the do..while loop.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,431
    Blog Entries
    7
    Rep Power
    20

    Default Re: Number Guessing Game issue

    Don't try to cram everything in one method. Suppose you have a method 'playGame()' that can play one single game. Also suppose you have a boolean method playAgain() that returns true if the user wants to play again. The following simple structure conducts everything:

    Java Code:
    do {
       playGame();
    }
    while (playAgain());
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: Number Guessing Game issue

    Maybe something like this:

    Java Code:
    while(true) {
    
    do {
      game
    }
    while();
    
    if (JOptionPane.showConfirmDialog(null, "Play again") != JOptionPane.OK_OPTION)
      break;
    }

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Number Guessing Game issue

    Quote Originally Posted by diamonddragon View Post
    Maybe something like this:

    Java Code:
    while(true) {
    
    do {
      game
    }
    while();
    
    if (JOptionPane.showConfirmDialog(null, "Play again") != JOptionPane.OK_OPTION)
      break;
    }
    I disagree. Cleaner to use a controlling boolean variable.

  7. #7
    gabrielpr12 is offline Member
    Join Date
    Nov 2011
    Posts
    41
    Rep Power
    0

    Default Re: Number Guessing Game issue

    I have changed the code, but now I keep going on a loop. After the user guesses correctly I ask if he wants to play again and it it supposed to accept input from the user, but instead it goes back to the do..while loop. Any ideas how do I break this loop in order to accept the user input?
    Java Code:
    import java.util.Scanner;
     
    public class RandomGuess3 
    {
          public static void main(String[] args) 
    		{
               
    			 boolean playAgain = true;	
    				do{
    						playGame();
    					}while (playAgain);	
          }
    		public static void playGame()
    		{
    			int secretNumber;
                secretNumber = (int) (Math.random() * 999 + 1);           
                Scanner keyboard = new Scanner(System.in);
                int guess;
    				String answer;
    				 boolean playAgain = true;
    				
                do {
                      System.out.print("Try to guess the secret number.\nType a number between 1-1000): ");
                      guess = keyboard.nextInt();
                      if (guess == secretNumber)
                            System.out.println("You guessed correctly! Congratulations!");
                      else if (guess < secretNumber)
                            System.out.println("The number you typed is lower than the secret number.");
                      else if (guess > secretNumber)
                            System.out.println("The number you typed is higher than the secret number.");
                	} while (guess != secretNumber);
    					
    					if (guess == secretNumber)
    							System.out.println("Do you want to play again?");
    							answer = keyboard.nextLine();
    							if(answer =="y")
    								playAgain = true;
    								else
    									playAgain = false;
    						
    						
    		}
    }

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,431
    Blog Entries
    7
    Rep Power
    20

    Default Re: Number Guessing Game issue

    In my hint playAgain() was a method ... and it prompts the user and returns true if the user wants to play another game ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: Number Guessing Game issue

    Quote Originally Posted by Fubarable View Post
    I disagree. Cleaner to use a controlling boolean variable.
    Than:

    Java Code:
    boolean control = true;
    while(control) {
     
    do {
      game
    }
    while();
     
    if (JOptionPane.showConfirmDialog(null, "Play again") != JOptionPane.OK_OPTION)
      control = false;
    }

  10. #10
    gabrielpr12 is offline Member
    Join Date
    Nov 2011
    Posts
    41
    Rep Power
    0

    Default Re: Number Guessing Game issue

    Quote Originally Posted by JosAH View Post
    In my hint playAgain() was a method ... and it prompts the user and returns true if the user wants to play another game ...

    kind regards,

    Jos
    Now, if playAgain() is another method, can I reference the playGame() method to get the answer?

  11. #11
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: Number Guessing Game issue

    Quote Originally Posted by gabrielpr12 View Post
    Now, if playAgain() is another method, can I reference the playGame() method to get the answer?
    You don't reference method, but assign reference variable to method return type.

  12. #12
    gabrielpr12 is offline Member
    Join Date
    Nov 2011
    Posts
    41
    Rep Power
    0

    Default Re: Number Guessing Game issue

    I changed the code again, but now when the user answers yes or no it does the same thing, it goes back to the main do..while loop. What am I missing in order to stop the loop? Is it something wrong with the boolean method playAgain()? Here is the updated code:

    Java Code:
    import java.util.Scanner;
     
    public class RandomGuess3 
    {
          public static void main(String[] args) 
    		{
               
    			 boolean playAgain = true;	
    				do{
    						playGame();
    					}while (playAgain());	
          }
    		public static void playGame()
    		{
    			int secretNumber;
                secretNumber = (int) (Math.random() * 999 + 1);           
                Scanner keyboard = new Scanner(System.in);
                int guess;
    				
                do {
                      System.out.print("Try to guess the secret number.\nType a number between 1-1000): ");
                      guess = keyboard.nextInt();
                      if (guess == secretNumber)
                            System.out.println("You guessed correctly! Congratulations!"+
    								"\n Do you want to play again?");
                      else if (guess < secretNumber)
                            System.out.println("The number you typed is lower than the secret number.");
                      else if (guess > secretNumber)
                            System.out.println("The number you typed is higher than the secret number.");
                	} while (guess != secretNumber);
    			}
    					
    	public static boolean playAgain()
    	{
    		String answer;
    		Scanner keyboard = new Scanner(System.in);
    		answer = keyboard.nextLine();
    		if (answer =="y");
    		return true;
    	}													
    }

  13. #13
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: Number Guessing Game issue

    Try:
    Java Code:
    if (answer.equals("y"))
       return true;
    else
       return false;
    Last edited by diamonddragon; 01-24-2012 at 10:58 PM.

  14. #14
    gabrielpr12 is offline Member
    Join Date
    Nov 2011
    Posts
    41
    Rep Power
    0

    Default Re: Number Guessing Game issue

    Quote Originally Posted by diamonddragon View Post
    Try:
    Java Code:
    if (answer.equals("y"))
       return true;
    else
       return false;
    works perfectly! thanks a lot!!!
    Last edited by gabrielpr12; 01-24-2012 at 11:05 PM.

  15. #15
    gabrielpr12 is offline Member
    Join Date
    Nov 2011
    Posts
    41
    Rep Power
    0

    Default Re: Number Guessing Game issue

    Here is the final working code:
    Java Code:
    import java.util.Scanner;
     
    public class RandomGuess3 
    {
          public static void main(String[] args) 
    		{
               
    			 boolean playAgain = true;	
    				do{
    						playGame();
    					}while (playAgain());	
          }
    		public static void playGame()
    		{
    			int secretNumber;
                secretNumber = (int) (Math.random() * 999 + 1);           
                Scanner keyboard = new Scanner(System.in);
                int guess;
    				
                do {
                      System.out.print("Try to guess the secret number.\nType a number between 1-1000): ");
                      guess = keyboard.nextInt();
                      if (guess == secretNumber)
                            System.out.println("You guessed correctly! Congratulations!"+
    								"\n Do you want to play again?");
                      else if (guess < secretNumber)
                            System.out.println("The number you typed is lower than the secret number.");
                      else if (guess > secretNumber)
                            System.out.println("The number you typed is higher than the secret number.");
                	} while (guess != secretNumber);
    			}
    					
    	public static boolean playAgain()
    	{
    		String answer;
    		Scanner keyboard = new Scanner(System.in);
    		answer = keyboard.nextLine();
    		if (answer.equals("y"))
    		return true;
    		else
    			return false;
    	}													
    }

Similar Threads

  1. Simple number guessing game but something's wrong???
    By JohnPringle83 in forum New To Java
    Replies: 11
    Last Post: 05-29-2011, 08:43 PM
  2. Reverse Number guessing game
    By rarman555 in forum New To Java
    Replies: 6
    Last Post: 04-24-2011, 01:46 AM
  3. Need help in Guessing Game
    By rose in forum Java Gaming
    Replies: 4
    Last Post: 10-27-2010, 10:43 PM
  4. Java - number guessing game
    By kev670 in forum Java Applets
    Replies: 3
    Last Post: 10-22-2010, 12:55 AM
  5. guessing game help
    By yasmin k in forum AWT / Swing
    Replies: 4
    Last Post: 10-31-2009, 05:37 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
  •