Stop 03 · Control Flow, Methods & ArraysProjects · 1/1

Number Guessing Game

Part of the Java Roadmap

In short

Time to build a real game. The computer picks a secret number, you guess, and it tells you "too high" or "too low". It is small, but it uses loops, conditions, methods, random numbers and input validation all at once.

A console game where the computer picks a random number and the player guesses until correct, getting 'too high' or 'too low' hints and a final attempt count. It practises loops, conditions, methods and input handling.

The jaadu at a colourful fair stall squints at a big glass jar full of round sweets, thinking hard with a finger on its chin.

Think of it like this

Guessing sweets in a jar

At a mela, a stall owner hides a number of sweets in a jar and you guess the count. After each guess, they say "more" or "less". Clever players halve the range every time and win in a few tries. Your game is the stall owner, and it keeps score.

The big ideas

What you need to know

Random numbers

new Random().nextInt(1, 101) returns 1 to 100 on Java 17+. The upper bound is excluded.

Nested loops

An inner loop for guesses inside an outer loop for rounds.

Small methods

readGuess and playRound keep main short and each piece testable.

Binary search thinking

Always guessing the middle of the remaining range wins in at most 7 tries for 1 to 100.

How it works

Game loop

  1. 1

    Pick a secret

    Use java.util.Random to choose a number from 1 to 100.

  2. 2

    Ask for a guess

    Read an int safely with Scanner and hasNextInt.

  3. 3

    Compare

    Too high, too low, or correct. Count every valid attempt.

  4. 4

    Repeat until correct

    A do-while or while loop keeps the game going.

  5. 5

    Play again?

    Show the attempt count and ask whether to start a new round.

See it in code

Walk through the code

java
1import java.util.Random;2import java.util.Scanner;3 4public class GuessGame {5    static final Scanner SC = new Scanner(System.in);6    static final Random RANDOM = new Random();7 8    static int readGuess() {9        System.out.print("Your guess (1-100): ");10        while (!SC.hasNextInt()) {11            SC.next();12            System.out.print("Numbers only: ");13        }14        return SC.nextInt();15    }16 17    static int playRound() {18        int secret = RANDOM.nextInt(1, 101);19        int attempts = 0;20        int guess;21        do {22            guess = readGuess();23            attempts++;24            if (guess > secret) System.out.println("Too high!");25            else if (guess < secret) System.out.println("Too low!");26        } while (guess != secret);27        return attempts;28    }29 30    public static void main(String[] args) {31        String again;32        do {33            int tries = playRound();34            System.out.println("Got it in " + tries + " tries!");35            System.out.print("Play again? (y/n) ");36            again = SC.next();37        } while (again.equalsIgnoreCase("y"));38    }39}

1/5One shared Scanner and Random for the whole program, as static constants.

The full game: safe input, a round loop that counts attempts and a replay loop.

Save yourself the pain

Common mistakes

Oops

new Random().nextInt(100) and expecting 1 to 100.

Do this instead

That returns 0 to 99. Use nextInt(1, 101), or nextInt(100) + 1 on older Java.

Oops

Creating the secret inside the guess loop.

Do this instead

Then it changes every guess. Pick it once per round, before the loop.

Your mission

Level up the game

~40 min

Quick check

Guessing smartly between 1 and 100, what is the most number of guesses you should ever need?

Go deeper

  1. 1Use Random to pick a number between 1 and 100 and a loop to keep asking.
  2. 2Move input reading and hint printing into separate methods.
  3. 3Add difficulty levels with a limited number of attempts and a best-score record.

Also part of these roadmaps

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge