Joined
·
10,440 Posts
I don't see what I am doing wrong. After I inputted the number 50 as my guess, I am suppose to get a message telling me if I am too high, too low or I am correct, but it's not.
If I enter the number 0 to quit, it does the same thing. I'm not even sure how to declare the 0 to be the number that quits the game.
Any ideas?
My error:
Instructions:
Quote:
My code:
Code:
[/CODE]

If I enter the number 0 to quit, it does the same thing. I'm not even sure how to declare the 0 to be the number that quits the game.
Any ideas?
My error:
Instructions:
Quote:
Design and implement an application that plays the Hi-Low guessing game with numbers. The program should pick a random number between 1 and 100 (inclusive), then keep asking the user to guess the number. ON each guess, report to the user that he or she is correct or that the guess is high or low. Keep accepting guesses until the user guesses correctly or quits. Use a sentinel value to determine wheater the user wants to quit. Count the number of guesses and report that value when the user guesses correctly. At the end of each game (by quitting or a correct guess), ask whether the user wants to play again. Keep playing games until the user chooses to stop. |
Code:
Code:
[CODE]
/* Anthony Dinh
Exercise 3.11
November 14, 2008
*/
import java.util.Random;
public class example
{
public static void main (String[] args)
{
final int MAX = 100;
int answer, guess;
EasyReader Keyboard = new EasyReader();
System.out.print ("I'm thinking of a number between 1 and "
+ MAX + ". Guess what it is: (or enter 0 to quit) ");
guess = Keyboard.readInt();
Random generator = new Random(); //Random generator. 1 to 100.
answer = generator.nextInt(MAX) +1;
if (guess == answer){ //If guess equals answer
System.out.println ("You got it! Good guessing!");
}if (guess == 0){ //Game ends
//System.out.println ("You have ended your game. Goodbye.");
}while (guess != answer && guess != 0){ //If guess and 0 is not answer, continue.
}if (guess > answer && guess != 0){ //If guess is higher than answer
System.out.println ("You guessed too high!");
guess = Keyboard.readInt();
}else{
if (guess < answer && guess != 0) //If guess is lower than answer
System.out.println ("You guessed too low!");
guess = Keyboard.readInt();
}if (guess == answer){ //If guess equals answer
System.out.println ("You got it! Good guessing!");
}if (guess == 0){ //Game ends
System.out.println ("You have ended your game. Goodbye.");
}
}
}