A Number Guessing Game is a simple Java project in which the computer generates a random number and the player tries to guess it within a limited number of attempts. After each incorrect guess, the program provides a hint to guide the player.
- Generates a random number within a specified range.
- Compares each user guess with the generated number.
- Provides “Guess Higher” or “Guess Lower” hints until the number is guessed or the attempt limit is reached.
How the Number Guessing Game Works
- The program generates a random number within a predefined range, such as 1 to 100.
- The player gets a limited number of attempts to guess the number.
- If the guess is too high, the program asks the player to guess lower; if it is too low, it asks the player to guess higher.
- The game ends when the player guesses correctly or uses all available attempts.
- If all attempts are exhausted, the program displays the correct number.
Approach
To implement the game, we need to follow the steps listed below:
- Generate a random number using the Math.random() method.
- Take user input using Scanner.
- Compare the guess with the random number and provide feedback.
- Limit the number of attempts using a loop.
- Handle edge cases (e.g., invalid inputs).
Implementation:
import java.util.Scanner;
public class Geeks {
public static void guessingNumberGame()
{
Scanner sc = new Scanner(System.in);
// Generate a random number between 1 and 100
int number = 1 + (int)(100 * Math.random());
// Number of attempts
int K = 5;
System.out.println(
"A number is chosen between 1 and 100.");
System.out.println(
"You have " + K
+ " attempts to guess the correct number.");
// Loop for K attempts
for (int i = 0; i < K; i++) {
System.out.print("Enter your guess: ");
int guess = sc.nextInt();
// Check conditions
if (guess == number) {
System.out.println(
" Congratulations! You guessed the correct number.");
sc.close();
// Exit function if guessed correctly
return;
}
else if (guess < number) {
System.out.println(
" The number is greater than " + guess);
}
else {
System.out.println(
" The number is less than " + guess);
}
}
// If the user runs out of attempts
System.out.println(
"You've exhausted all attempts. The correct number was: "
+ number);
sc.close();
}
public static void main(String[] args)
{
guessingNumberGame();
}
}
Output:

Note: You can also use Random class from java.util instead of Math.random() to generate random numbers in Java.
Random random = new Random();
int number = random.nextInt(100) + 1;
Number Guessing Game with Unlimited Rounds and Score Tracking
Approach:
- After Exhausting K attempts, the user is given another chance to continue the game.
- The game tracks the score (number of attempts taken to guess correctly).
- The game continues until the user guesses the correct number.
Implementation:
The below Java program demonstrates the number-guessing game with Unlimited Rounds and Score Tracking.
// Number guessing game with
// Unlimited Rounds and Score Tracking
import java.util.Scanner;
public class Geeks {
public static void guessingNumberGame()
{
Scanner sc = new Scanner(System.in);
// Generate a random number between 1 and 100
int number = 1 + (int)(100 * Math.random());
// Track the number of attempts
int attempts = 0;
// Maximum attempts per round
int K = 5;
boolean guessedCorrectly = false;
System.out.println(
"A number is chosen between 1 and 100.");
System.out.println(
"You have " + K
+ " attempts per round to guess the correct number.");
while (!guessedCorrectly) {
// Give the user K attempts per round
for (int i = 0; i < K; i++) {
System.out.print("Enter your guess: ");
int guess = sc.nextInt();
attempts++; // Increment attempt count
if (guess == number) {
System.out.println(
"Congratulations! You guessed the correct number in "
+ attempts + " attempts.");
guessedCorrectly = true;
break;
}
else if (guess < number) {
System.out.println(
"The number is greater than "
+ guess);
}
else {
System.out.println(
"The number is less than " + guess);
}
}
if (!guessedCorrectly) {
// Ask the user if they want to continue
// after exhausting K attempts
System.out.println("You have used all " + K
+ " attempts.");
System.out.print(
"Do you want to continue guessing? (yes/no): ");
String response = sc.next();
if (!response.equalsIgnoreCase("yes")) {
System.out.println(
"Game Over! The correct number was: "
+ number);
break;
}
}
}
sc.close();
}
public static void main(String[] args)
{
guessingNumberGame();
}
}
Note: The for loop manages the fixed number of attempts per round, whereas the while loop allows the game to continue until the user guesses the correct number or chooses to quit.
Output:
