/* This application plays a Hi-Lo guessing game with numbers. The program picks a random number between 1 and 100 (inclusive), then repeatedly prompts the user to guess the number.
On each guess, the user is informed whether they are correct or if the guess is incorrect, whether it is high or low.
The program will continue accepting guesses until the user guesses correctly or chooses to quit. It will also count and report the number of guesses by the user to correctly guess the chosen number. If a guess is out of the range (1 to 100), the user will be advised to correct their guess and the guess will not be counted. At the end of each game (by quitting or a correct guess), the user will be asked to play again. If they choose to continue playing another game the program will cycle until the user chooses to stop. */
System.out.println("Let's play a guessing game!");
System.out.println("Pick a number between " + MIN + " and " + MAX +
" (inclusive). ");
guess = scan.nextInt();
while
{
if (guess < MIN || guess > MAX)
{
System.out.println("Your guess is out of range!");
System.out.println("Pick a number between " + MIN + " and " + MAX +
" (inclusive). ");
}
count++;
else if(guess == answer)
{
System.out.println("CONGRATULATIONS! The number was " + answer +
". You guessed correctly in " + count + " tries!");
System.out.println("Would you like to play again?");
System.out.print("Type \"Y\" to play and \"N\" to quit. ");
String play = scan.nextLine();
if(play.equalsIgnoreCase("Y"))
{
System.out.println("Game on!");
}
else if(play.equalsIgnoreCase("N"))
{
System.out.println("Thanks for playing! Good-bye.");
return;
}
}
else if (guess < answer)
{
System.out.println("Sorry, your guess " + guess + " is too low. "
+ "Try again!");
}
else if (guess > answer)
{
System.out.println("Sorry, your guess " + guess + " is too high. "
+ "Try again!");
}
}
}
}
Aucun commentaire:
Enregistrer un commentaire