Post

Guess the Number in Java

Guess the Number in Java

Overview

This program prompts the user to guess a secret number within a user-defined range and limited number of attempts.
It provides hints (“too high” or “too low”) and allows replay after each round.

Features

  • User-defined range (1 to N)
  • User-defined maximum guesses
  • Random number generation using Math.random()
  • Feedback on each attempt
  • Option to replay multiple rounds
  • Graceful termination message
  • Safe input handling via try-with-resources

Code Implementation

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import java.util.Scanner;

public class GuessTheNumber {
    public static void main(String[] args) {
        try (Scanner input = new Scanner( System.in )) {
            boolean playAgain = true;

            while (playAgain) {
                System.out.println();
                System.out.print("Enter the maximum number for the secret number: ");
                int N = input.nextInt();
                System.out.println();
                System.out.print("Enter the maximum number of guesses allowed: ");
                int maxGuesses = input.nextInt();
                System.out.println();
                int secretNumber = (int) (N * Math.random()) + 1;
                int guessCount = 0;
                boolean guessedCorrectly = false;

                while (guessCount < maxGuesses && !guessedCorrectly) {
                    System.out.print("Enter your guess: ");
                    int guess = input.nextInt();
                    guessCount++;
                    System.out.println();

                    if (guess == secretNumber) {
                        System.out.println("Correct! You guessed the number in " + guessCount + " tries.");
                        guessedCorrectly = true;
                        System.out.println();

                    } else if (guess < secretNumber) {
                        System.out.println("Too low!");
                        System.out.println();

                    } else {
                        System.out.println("Too high!");
                        System.out.println();
                    }
                }
                if (!guessedCorrectly) {
                    System.out.println("Sorry, you did not guess the number. The secret was " + secretNumber + ".");
                }

                System.out.println();
                System.out.print("Do you want to play again? (yes/no): ");
                String response = input.next();
                System.out.println();

                if (!response.equalsIgnoreCase("yes")) {
                    playAgain = false;
                    System.out.println("Thanks for playing!");
                    System.out.println();
                }
            }
        } catch (Exception e) {
            System.err.println("Error reading input: " + e.getMessage());
        }
    }
}

Sample Output

Console output from the Guess the Number Java program

Notes

  • Designed for console execution in Java (no GUI)
  • Assumes valid numeric input (exception handling out of scope)
  • Ideal for beginners learning loops and branching
  • Compatible with Java 17+

Triangles