
Program 2 Java Due: October 11, 2005
Write a Java program that allows the user to play a number guessing game with the
program. Your program will generate a random number between 0 and 100 inclusive.
The user does not know the value of this number and she must guess the value while the
program runs in a loop. After each guess report to the user if the guess was “too high”,
“too low”, or “correct”. After the user has guessed the correct value print out the number
of guesses it took to get the number and then ask to user if they want to play again.
Use the value “No” or “no” to indicate a sentinel value to quit the loop and end the
program. The additional feature to add is to calculate the average of how many attempts
it took to guess the correct number for all games played from start until the “No” is
entered. In other words let’s assume that the following 3 games were played and the
user’s guesses were followed by the programs responses:
Computer’s Random Number My Guess Output
17 Enter a value!
20 Too high
16 Too low
17 Correct, 3 guesses
Continue ?
Y
63 Enter a value!
47 Too low
72 Too high
64 Too high
62 Too low
63 Correct, 5 guesses
Continue ?
Y
37 Enter a value!
52 Too high
37 Correct, 2 guesses
Continue ?
No
Average = 3.33 guesses,
in 3 games
Good Bye!
Turn in your program according to the submission guidelines. Include an answer to the
following question on a separate document (Word file is OK). “What guessing
strategy will give you the best chance in guessing the correct number in the least number
of attempts?”
The computer’s number is randomly created between 1 and 100. Try this strategy versus
other ones and see which ones give you the minimal number of guesses on the average.
Can you get close to six on the average?