This is a game between you and the computer. The computer is going to randomly select an integer from 1 to 100. You have to guess the number by making guesses until you find the number that the computer chose. The computer will tell you if each guess is too high or too low. You win if you can guess the number within six tries.
Note: This is a good game to code because it uses random numbers, loops, and input from the user in a short program. You’ll learn how to convert values to different data types, and why you would need to do this. Since this program is a game, we’ll call the user the player. But “user” would be correct too.
Sample Output
Hello! What is your name?
Albert
Well, Albert, I am thinking of a number between 1 and 20.
Take a guess.
10
Your guess is too high.
Take a guess.
2
Your guess is too low.
Take a guess.
4
Good job, Albert! You guessed my number in 3 guesses!
Step 1. Start with simple java program structure
Let's call the class "NumberGuessingGame" and add empty main function. It's completely valid program you can compile and run, but it doesn't print anything to the console yet.
public class NumberGuessingGame {
public static void main(String[] args) {
}
}
Step 2. Generate Secret number
To propose a secret number, we declare a variable secretNumber of type int and use Math.random() function to give it random value in range 1..100.
public class NumberGuessingGame {
public static void main(String[] args) {
int secretNumber;
secretNumber = (int) (Math.random() * 100);
System.out.println("Secret number is " + secretNumber); // to be removed later
}
}
Compile and run the program to see the output.
Note that, this program exposes secret number to player at the moment, but we will remove this statement in the final version.
Step 3. Asking user for a guess
In order to get input from user, we declare another variable guess of type int. Code, reading input from user is not to be discussed in detail here, so take it on trust.
import java.util.Scanner;
public class NumberGuessingGame {
public static void main(String[] args) {
int secretNumber;
secretNumber = (int) (Math.random() * 99 + 1);
System.out.println("Secret number is " + secretNumber); // to be removed later
Scanner keyboard = new Scanner(System.in);
int guess;
System.out.print("Enter a guess: ");
guess = keyboard.nextInt();
System.out.println("Your guess is " + guess);
}
}
Suppose your output will be like this:
Secret number is 78
Enter a guess: 55
Your guess is 55
Step 4. Checking if guess is right
Now let's check if human player's guess is right. If so program should print corresponding message. Otherwise, tell user that a guess is smaller/greater than the proposed number. To test the program let's try all three cases (remember that we peeked the secret number).
import java.util.Scanner;
public class NumberGuessingGame {
public static void main(String[] args) {
int secretNumber;
secretNumber = (int) (Math.random() * 99 + 1);
System.out.println("Secret number is " + secretNumber); // to be removed later
Scanner keyboard = new Scanner(System.in);
int guess;
System.out.print("Enter a guess: ");
guess = keyboard.nextInt();
System.out.println("Your guess is " + guess);
if (guess == secretNumber)
System.out.println("Your guess is correct. Congratulations!");
else if (guess < secretNumber)
System.out.println("Your guess is smaller than the secret number.");
else if (guess > secretNumber)
System.out.println("Your guess is greater than the secret number.");
}
}
Try compile and run the program. What is your output?
Step 5. Add tries
At the moment user has only one attempt to guess a number, which is, obvious, not sufficient. Our next step is about giving user as many attempts as one needs. For this purpose let's use do-while loop, because user must enter a guess at least once.
import java.util.Scanner;
public class NumberGuessingGame {
public static void main(String[] args) {
int secretNumber;
secretNumber = (int) (Math.random() * 99 + 1);
System.out.println("Secret number is " + secretNumber); // to be removed later
Scanner keyboard = new Scanner(System.in);
int guess;
do {
System.out.print("Enter a guess: ");
guess = keyboard.nextInt();
System.out.println("Your guess is " + guess);
if (guess == secretNumber)
System.out.println("Your guess is correct. Congratulations!");
else if (guess < secretNumber)
System.out.println("Your guess is smaller than the secret number.");
else if (guess > secretNumber)
System.out.println("Your guess is greater than the secret number.");
} while (guess != secretNumber);
}
}
The statements inside do-while block will repeat until user's guess same as the secret number.
Step 6. Final step. Make it glow
Just before we consider the program to be completed, let's remove the code that used for debug purposes.
import java.util.Scanner;
public class NumberGuessingGame {
public static void main(String[] args) {
int secretNumber;
secretNumber = (int) (Math.random() * 99 + 1);
Scanner keyboard = new Scanner(System.in);
int guess;
do {
System.out.print("Enter a guess (1-100): ");
guess = keyboard.nextInt();
if (guess == secretNumber)
System.out.println("Your guess is correct. Congratulations!");
else if (guess < secretNumber)
System.out.println("Your guess is smaller than the secret number.");
else if (guess > secretNumber)
System.out.println("Your guess is greater than the secret number.");
} while (guess != secretNumber);
}
}
Check, if user enters the number in range 1..100 and force one to re-enter a guess in case it is out of bounds.
Limit the number of attempts to 3 attempts only.