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?
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..
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)? ");
}
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.
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....
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.