User Tools

Site Tools


cs175:loops_lab_-_number_guessing_game

GuessNumber - a number guessing game


Write a program GuessNumber.java that plays a number guessing game.

The program will generate a random integer between 1 and 100, using the randbetween method.

Your program will ask the user to guess the value of the number between 1 and 100.

If the user's guess is within 5 of being correct, output "HOT".

If the user's guess is within 10 of being correct, output "VERY WARM".

If the user's guess is within 20 of being correct, output "WARM".

If the user's guess is within 40 of being correct, output "LUKEWARM".

If the user's guess is not within 40 of being correct, output "COLD".

Continue until the user has correctly input the correct number.

Hints:

  • You'll want to use an if, else if, else structure to evaluate the correctness of the player's guesses.
  • Compute the difference between the player's guess and the correct number, and get the absolute value of the difference. Use the difference in your if, else if conditions.
  • A loop is needed to allow the player to enter a guess repeatedly. But again, I recommend worrying about the loop last. Make sure that your if, else if, else structure works correctly first.

Possible improvements that could be made to this game:

  • Count the number of tries that it took to correctly guess the number.
  • Output “Getting warmer” or “Getting colder”, depending on whether the player's next guess is closer or further away from the correct number, compared to their last guess.
  • Allow the player to enter -1 to quit the game immediately.

cs175/loops_lab_-_number_guessing_game.txt · Last modified: 2014/11/20 20:31 by jchung

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki