Quick help needed. Code runs, but one problem cannot be solved  
Author Message
digibooster@gmail.com





PostPosted: 2006-11-15 7:30:00 Top

java-programmer, Quick help needed. Code runs, but one problem cannot be solved Hey, guys. I designed this code for guessing numbers.

The program should pick a random number between 1 and 10, inclusive
both. Then repeatedly prompt 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. Continue accepting guesses until the user guesses
correctly or choose to quit. Count the number of guesses and report
that value when the user guesses correctly. At the end of each game,
prompt to determine whether the user wants to play again. Continue
playing games until the user chooses to stop

Here is the problem, I let the random number print, so I know what
number is it. But when I input that number, instead of showing up
"Congratulations...", it shows up the correct number I entered is too
high. Please let me know what is going on. Thank you


//Code started!



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

public class HighLow
{
//
// Plays the Hi-Lo guessing game with the user
//
public static void main(String[] args)
{
boolean done = false;
boolean playAgain = true;
Random numberGenerator = new Random();
int randomNumber, numberOfGuesses, userNumber;
Scanner scan = new Scanner(System.in);

//
// Constants for messages to the user
//
final String guessMessage = "Enter your guess (-1 to quit): ";
final String tooHigh = "Sorry, the number you entered is higher than
my number.";
final String tooLow = "Sorry, the number you entered is lower than my
number.";
final String justRight = "Congratulations! You guessed my number!";
final String playAgainMessage = "Play again?";

while (playAgain) {
//
// Pick a random number
//
randomNumber = numberGenerator.nextInt(10) + 1;
System.out.println("The correct number is " +randomNumber);

//
// Start the guessing game
//
System.out.print(guessMessage);
userNumber = scan.nextInt();
numberOfGuesses = 1;

if (userNumber > randomNumber)
{
System.out.println(tooHigh);
numberOfGuesses += 1;
}
if (userNumber <0 && userNumber!=-1)
{
System.out.println("1-10 only");
}
if (userNumber < randomNumber && userNumber > 0)
{
System.out.println(tooLow);
numberOfGuesses += 1;
}
if (userNumber == randomNumber)
{
System.out.println(justRight);
numberOfGuesses += 1;
System.out.println("It taks you " +numberOfGuesses+ " times to
guess right");
}
//
// Check to see if the user wants to stop guessing
//
if (userNumber == -1)
done = true;
//
// Check to see if the user wants to play again
//
System.out.print("Would you like to play again? (y/n)? ");

if (scan.next().equals("n"))
playAgain = false;
else
done = false;
} // while loop

System.out.println("Thanks for playing! Bye!" );


} //method main
} //class HiLoGame

 
Kyle Vasatka





PostPosted: 2006-11-15 15:25:00 Top

java-programmer >> Quick help needed. Code runs, but one problem cannot be solved Check your 4 "if" statements within your "start the guessing game".
You should have an "else" within there, not all "if" statements. Not
sure if that will fix your problem though.


email***@***.com wrote:
> Hey, guys. I designed this code for guessing numbers.
>
> The program should pick a random number between 1 and 10, inclusive
> both. Then repeatedly prompt 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. Continue accepting guesses until the user guesses
> correctly or choose to quit. Count the number of guesses and report
> that value when the user guesses correctly. At the end of each game,
> prompt to determine whether the user wants to play again. Continue
> playing games until the user chooses to stop
>
> Here is the problem, I let the random number print, so I know what
> number is it. But when I input that number, instead of showing up
> "Congratulations...", it shows up the correct number I entered is too
> high. Please let me know what is going on. Thank you
>
>
> //Code started!
>
>
>
> import java.util.Random;
> import java.util.Scanner;
>
> public class HighLow
> {
> //
> // Plays the Hi-Lo guessing game with the user
> //
> public static void main(String[] args)
> {
> boolean done = false;
> boolean playAgain = true;
> Random numberGenerator = new Random();
> int randomNumber, numberOfGuesses, userNumber;
> Scanner scan = new Scanner(System.in);
>
> //
> // Constants for messages to the user
> //
> final String guessMessage = "Enter your guess (-1 to quit): ";
> final String tooHigh = "Sorry, the number you entered is higher than
> my number.";
> final String tooLow = "Sorry, the number you entered is lower than my
> number.";
> final String justRight = "Congratulations! You guessed my number!";
> final String playAgainMessage = "Play again?";
>
> while (playAgain) {
> //
> // Pick a random number
> //
> randomNumber = numberGenerator.nextInt(10) + 1;
> System.out.println("The correct number is " +randomNumber);
>
> //
> // Start the guessing game
> //
> System.out.print(guessMessage);
> userNumber = scan.nextInt();
> numberOfGuesses = 1;
>
> if (userNumber > randomNumber)
> {
> System.out.println(tooHigh);
> numberOfGuesses += 1;
> }
> if (userNumber <0 && userNumber!=-1)
> {
> System.out.println("1-10 only");
> }
> if (userNumber < randomNumber && userNumber > 0)
> {
> System.out.println(tooLow);
> numberOfGuesses += 1;
> }
> if (userNumber == randomNumber)
> {
> System.out.println(justRight);
> numberOfGuesses += 1;
> System.out.println("It taks you " +numberOfGuesses+ " times to
> guess right");
> }
> //
> // Check to see if the user wants to stop guessing
> //
> if (userNumber == -1)
> done = true;
> //
> // Check to see if the user wants to play again
> //
> System.out.print("Would you like to play again? (y/n)? ");
>
> if (scan.next().equals("n"))
> playAgain = false;
> else
> done = false;
> } // while loop
>
> System.out.println("Thanks for playing! Bye!" );
>
>
> } //method main
> } //class HiLoGame

 
Oliver Wong





PostPosted: 2006-11-16 1:26:00 Top

java-programmer >> Quick help needed. Code runs, but one problem cannot be solved
<email***@***.com> wrote in message
news:email***@***.com...
> Hey, guys. I designed this code for guessing numbers.
>
> The program should pick a random number between 1 and 10, inclusive
> both. Then repeatedly prompt 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. Continue accepting guesses until the user guesses
> correctly or choose to quit. Count the number of guesses and report
> that value when the user guesses correctly. At the end of each game,
> prompt to determine whether the user wants to play again. Continue
> playing games until the user chooses to stop
>
> Here is the problem, I let the random number print, so I know what
> number is it. But when I input that number, instead of showing up
> "Congratulations...", it shows up the correct number I entered is too
> high. Please let me know what is going on. Thank you

[code snipped]

If I enter the number displayed (e.g. if it says "5" and I enter "5"),
the program tells me that I guessed correctly. However, it incorrectly tells
me that I used 2 guesses. Also, if I enter a number too low (e.g. "2"), the
game tells me I guessed too low, and them immediately asks me if I want to
play again, rather than asking for a second guess.

- Oliver


 
 
Kyle Vasatka





PostPosted: 2006-11-16 4:48:00 Top

java-programmer >> Quick help needed. Code runs, but one problem cannot be solved Make sure your counter starts at 0 and increments after each guess.

On Nov 15, 11:26 am, "Oliver Wong" <email***@***.com> wrote:
> <email***@***.com> wrote in messagenews:email***@***.com...
>
> > Hey, guys. I designed this code for guessing numbers.
>
> > The program should pick a random number between 1 and 10, inclusive
> > both. Then repeatedly prompt 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. Continue accepting guesses until the user guesses
> > correctly or choose to quit. Count the number of guesses and report
> > that value when the user guesses correctly. At the end of each game,
> > prompt to determine whether the user wants to play again. Continue
> > playing games until the user chooses to stop
>
> > Here is the problem, I let the random number print, so I know what
> > number is it. But when I input that number, instead of showing up
> > "Congratulations...", it shows up the correct number I entered is too
> > high. Please let me know what is going on. Thank you[code snipped]
>
> If I enter the number displayed (e.g. if it says "5" and I enter "5"),
> the program tells me that I guessed correctly. However, it incorrectly tells
> me that I used 2 guesses. Also, if I enter a number too low (e.g. "2"), the
> game tells me I guessed too low, and them immediately asks me if I want to
> play again, rather than asking for a second guess.
>
> - Oliver