Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Aug 2005
    Posts
    61
    Thanks
    0
    Thanked 0 Times in 0 Posts

    [Java] Help with guessing game.

    Code:
    import TerminalIO.*;
    
    public class GuessingGame
    {
    	public static void main (String[] args)
    	{
    		KeyboardReader reader = new KeyboardReader();
    		
    		double randomNumber,													//The number to be guessed
    		guessedNumber;															//The number guessed by the player.
    		char runAgain = 'y';													//Run again option
    		
    		randomNumber = Math.random() * (100);									//Determines the number
    		
    		while (runAgain == 'y' || runAgain == 'Y')								//Play again?
    		{
    			guessedNumber = reader.readDouble("Please pick a number between 1 and 100");														//
    			
    			if (guessedNumber != randomNumber)									//Loop untill the user guesses the correct number
    			{																	//
    				if (guessedNumber < randomNumber)								//
    					System.out.println("Too low!\nGuess again!");				//
    				else if (guessedNumber > randomNumber)							//												//
    					System.out.println("Too high\nGuess again!");				//
    				else if (guessedNumber == randomNumber)							//
    					System.out.println("You got it!");
    					
    			guessedNumber = reader.readDouble("Please pick a number between 1 and 100");
    			}
    				
    			runAgain = reader.readChar("Play again (y/n)?");					//Play again?
    		}	
    		
    	}
    }
    I am trying to get it to keep letting the user guess while the number is wrong... What am I doing wrong here?

  • #2
    Senior Coder TheShaner's Avatar
    Join Date
    Sep 2005
    Location
    Orlando, FL
    Posts
    1,126
    Thanks
    2
    Thanked 40 Times in 40 Posts
    You need to restructure big time. You only enter that if statement if the guessed number and random number aren't equal, but then inside of it, you check to see if they're equal? And then you assume that the code will loop in the if statement. You could have set it up to have an inner loop that loops until the guessed number and random number are equal or you could have set it up like the one below. You should really iterate on paper and see where the problem is. It helps a lot. But here is the corrected code anyway.

    Code:
    while (runAgain == 'y' || runAgain == 'Y')
    {
    
      guessedNumber = reader.readDouble("Please pick a number between 1 and 100");
    
      if (guessedNumber < randomNumber)
        System.out.println("Too low!\nGuess again!");
      else if (guessedNumber > randomNumber)
        System.out.println("Too high\nGuess again!");
      else if (guessedNumber == randomNumber)
      {
        /* 
            Since you got the number, ask to play again. If it is y, 
            then you'll notice the that the while loop will continue,
            and a new randomNumber will be generated.
            If it isn't y, then it'll break the loop before asking for a
            number again. 
        */
        System.out.println("You got it!");
        runAgain = reader.readChar("Play again (y/n)?");
        if runAgain == 'y'
          randomNumber = Math.random() * (100);
      }
    
    }
    -Shane
    Last edited by TheShaner; 09-28-2005 at 05:29 PM.

  • #3
    New Coder
    Join Date
    Aug 2005
    Posts
    61
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I've decided to use a while statement to loop, but when I compile and run, it enters an infinate loop, instead of doing what I want to do. Obviously I am doing something wrong. Heres the pseducode:
    Code:
    if (number is correct) System.out.println("Good job")
    
    else if (number is too high) System.out.println("Too high, guess again")
    (then loop and give user chance to guess again)
    
    else if (number is too low) System.out.println("Too low, guess again")
    (then loop and give user chance to guess again)
    Heres what I have:
    Code:
    while (runAgain == 'y' || runAgain == 'Y')				
    {
    guessedNumber = reader.readDouble("Please pick a number between 1 and 100: ");
    			
    while (guessedNumber != randomNumber)
         {
              if (guessedNumber < randomNumber)				
              System.out.println("Too low!\nGuess again!");
    
              else if (guessedNumber > randomNumber)
              System.out.println("Too high\nGuess again!");
    
              else if (guessedNumber == randomNumber)
              System.out.println("You got it!");
         }	
    runAgain = reader.readChar("Play again (y/n)? ");			
    }

  • #4
    Senior Coder TheShaner's Avatar
    Join Date
    Sep 2005
    Location
    Orlando, FL
    Posts
    1,126
    Thanks
    2
    Thanked 40 Times in 40 Posts
    You need this line:

    Code:
    guessedNumber = reader.readDouble("Please pick a number between 1 and 100: ");
    Inside your second while loop. That's why you have the infinite loop. You never ask for the number again, so it keeps processing on the very same number.

    -Shane

  • #5
    New Coder
    Join Date
    Aug 2005
    Posts
    61
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I finally got it. Instead of using Math.random, I used the random Int generator.
    Code:
    import TerminalIO.*;
    import java.util.Random;
    
    public class project6_1GuessingGame
    {
    	public static void main (String[] args)
    	{
    		KeyboardReader reader = new KeyboardReader();
    		Random random = new Random();
    		
    		int randomNumber,
    		guessedNumber = 0;
    		char runAgain = 'y';
    		
    		randomNumber = random.nextInt(101);									
    		System.out.println(randomNumber);
    		while (runAgain == 'y' || runAgain == 'Y')
    		{
    			guessedNumber = reader.readInt("Please pick a number between 1 and 100: ");
    			if (guessedNumber < randomNumber)
    					System.out.println("Too low!\nGuess again!" +"\n");
    				if (guessedNumber > randomNumber)
    					System.out.println("Too high\nGuess again!" +"\n");
    				if (guessedNumber == randomNumber)
    					System.out.println("You got it!" +"\n");
    			while (guessedNumber != randomNumber)
    			{
    				guessedNumber = reader.readInt("Please pick a number between 1 and 100: ");
    				if (guessedNumber < randomNumber)
    					System.out.println("Too low!\nGuess again!" +"\n");
    				if (guessedNumber > randomNumber)
    					System.out.println("Too high\nGuess again!" +"\n");
    				if (guessedNumber == randomNumber)
    					System.out.println("You got it!" +"\n");
    			}	
    			runAgain = reader.readChar("Play again (y/n)? ");
    			if (runAgain == 'n' || runAgain == 'N')
    			{
    				System.exit(0);
    			}
    		}
    	}
    }
    Thanks for the help guys

  • #6
    New to the CF scene
    Join Date
    Oct 2010
    Location
    philipines
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    how to save in vb?

    need help here...

  • #7
    $object->toCD-R(LP); vinyl-junkie's Avatar
    Join Date
    Jun 2003
    Posts
    3,073
    Thanks
    2
    Thanked 22 Times in 22 Posts
    Quote Originally Posted by xduh9 View Post
    need help here...
    See post #4 in this thread.
    Music Around The World - Collecting tips, trade
    and want lists, album reviews, & more
    SNAP to it!

  • #8
    Banned
    Join Date
    Oct 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i have learnt something different by viewing the post. and thanks TheShaner!

  • #9
    New Coder
    Join Date
    Oct 2010
    Location
    Mountain View
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I do have to create a similar program but I got 2 problems. In my case is the user that chose the numer rage for the guessing game...I don't know how to do that. Second I have to keep a counter for all the user try. I need two class a main class with the gui and game class that represents one play of the Game....

    Someone can give me some idea how to proceed ?

    Thanks

  • #10
    Gox
    Gox is offline
    Regular Coder Gox's Avatar
    Join Date
    May 2006
    Location
    Ontario, Canada
    Posts
    392
    Thanks
    2
    Thanked 20 Times in 20 Posts
    Quote Originally Posted by Superteo1987 View Post
    I do have to create a similar program but I got 2 problems. In my case is the user that chose the numer rage for the guessing game...I don't know how to do that. Second I have to keep a counter for all the user try. I need two class a main class with the gui and game class that represents one play of the Game....

    Someone can give me some idea how to proceed ?

    Thanks
    It is best if you start a new thread. Clearly state the problem(s) you're having, and post your code so that we can help identify where you went wrong.


  •  

    Posting Permissions

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