For your second program, you will create a numeric guessing game. This will practice using while
loops, parsing user input from the Terminal while the program is running using a Scanner
, and Exception
handling.
In your program, the computer will pick a random number between 1 and 1000, and then the user will try to guess what it is until they guess the correct number. Each time the user guesses incorrectly, the program should provide a hint that the guessed number is either too high or too low. For example, a run of the program might look like:
Let's play a guessing game!
I'm thinking of a number between 1 and 1000
What number am I thinking of?
Enter a guess: 500
Too low!
Enter a guess: 750
Too high!
Enter a guess: 625
Too low!
Enter a guess: -72
Too low!
Enter a guess: kittens
That's not a number, please try again.
Enter a guess: 630
Too high!
Enter a guess: 629
You guessed my number! You had 6 number guesses.
Inside your GitHub repository, you will find a file called GuessingGame.java
. Open this file in Visual Studio Code by either using the “File” > “Open” dialog box, or double click on the GuessingGame.java
file on the left side of your IDE in the “Explorer” area. Once again, the skeleton of the program is already provided for you in this file. All you need to do is implement the program in the public static void main(String[] args)
function.
To have the computer choose a random number, you will need to create a Random
object that will let you generate random numbers. At the very top of your program (outside of the GuessingGame
class and the main
function), you’ll need to add an import
statement so that Java knows where to find the Random
class:
import java.util.Random;
In your main function, you can then create an instance of the Random
class and use its nextInt(n)
method to choose a random number between 1
and n
:
Random rnd = new Random();
int target = rnd.nextInt(1000) + 1;
Now you’ll want to loop until the user guesses your number. We don’t know in advance how many guesses the user might need to win the game, so we should use a while
loop. Keep in mind that you can use break
to exit the loop or continue
to immediately go to the next iteration of a loop.
Use a Scanner
object to read from System.in
which will capture keyboard input. This is very similar to input() in Python, as well as using a Scanner
with a single String as in Warmup. The syntax is:
Scanner userInput = new Scanner(System.in);
If the line doesn’t start with a number (i.e., calling the Scanner
’s hasNextInt()
method returns false
), then you should print a message out to the user, use the Scanner
’s next()
method to ignore the non-number guess, and finally loop back to where you read an entire line. For an example, look at Warmup.
If the user correctly guesses the randomly chosen number target
, then the game is over. If they incorrectly guess, then you should give them a hint by comparing their guess to the random
number target using an if-else
statement.
Keep track of the number of numeric guesses the user makes and let them know how many tries it took them at the end of the game.
In your terminal window, type:
javac GuessingGame.java
java GuessingGame
Once you’ve finished your program, don’t forget to add
and commit
your changes to git, then push
them to your GitHub repository. Instructions for how to do so can be found earlier in the Warmup.