Java Source Code for The HiLo Game



The purpose of the HiLo (or Hi-Lo) game is to guess the right number in a certain number of attempts. If the guess is too high the word HI is printed out and if the guess is too low the word LO is printed out.
In this example you have six attempts to guess the right number. The code consists of two classes, one main class and one class that contains the actual game logic.
The HiLo class generates its secret number, between 1-100, with the java.util.Random class.

Here’s a short description of every method in the HiLo class:
HiLo() – the constructor. Creates the Random object and the stream to read user input.
Start() – the entry point to the game, loops until the user don’t want to play anymore.
describeRules() – prints out the rules for the HiLo game.
generateSecretNumber() – generates the number to guess.
playGame() – loops at the most 6 times and get the input from the user. If the guess was correct the loop stops.
prompt() – asks the user if he / she wants to play again and take care of input.
getNextGuess() – gets the next guess from the user.

The main class creates a new instance of the HiLo class and calls its start() method.

The output from a game could look like this:







Search for more Java info on this site here:
Custom Search


Any thoughts or questions?