Learn Java Basics
About Lesson

Build a program that generates a random number between 1 and 100, and prompts the user to guess the number. The program should provide feedback on whether the guess is too high or too low.


import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        int randomNumber = (int) (Math.random() * 100) + 1;
        boolean hasWon = false;
        System.out.println("I'm thinking of a number between 1 and 100. Can you guess it?");
        
        Scanner scanner = new Scanner(System.in);
        for (int i = 10; i > 0; i--) {
            System.out.println("You have " + i + " guess(es) left. Guess the number:");
            int guess = scanner.nextInt();
            
            if (guess == randomNumber) {
                hasWon = true;
                break;
            } else if (guess < randomNumber) {
                System.out.println("Too low!");
            } else {
                System.out.println("Too high!");
            }
        }
        
        if (hasWon) {
            System.out.println("Congratulations, you guessed the number!");
        } else {
            System.out.println("Game over. The number was " + randomNumber);
        }
    }
}

Output:

I'm thinking of a number between 1 and 100. Can you guess it?
You have 10 guess(es) left. Guess the number:
50
Too low!
You have 9 guess(es) left. Guess the number:
90
Too high!
You have 8 guess(es) left. Guess the number:
75
Too high!
You have 7 guess(es) left. Guess the number:
60
Too low!
You have 6 guess(es) left. Guess the number:
70
Too high!
You have 5 guess(es) left. Guess the number:
65
Too low!
You have 4 guess(es) left. Guess the number:
68
Too high!
You have 3 guess(es) left. Guess the number:
66
Too low!
You have 2 guess(es) left. Guess the number:
67
Congratulations, you guessed the number!


Explanation

Here is an explanation of each token in the provided code:

import java.util.Scanner; – This line imports the Scanner class from the java.util package, which is used to read user input.

public class Main { – This line declares a new class called Main.

public static void main(String[] args) { – This line declares the main method, which is the entry point for the program.

int randomNumber = (int) (Math.random() * 100) + 1; – This line generates a random integer between 1 and 100 and assigns it to the randomNumber variable.

boolean hasWon = false; – This line declares a boolean variable called hasWon and initializes it to false.

System.out.println("I'm thinking of a number between 1 and 100. Can you guess it?"); – This line prints a message to the console.

Scanner scanner = new Scanner(System.in); – This line creates a new Scanner object that reads input from the standard input stream (i.e., the console).

for (int i = 10; i > 0; i--) { – This line declares a for loop that runs 10 times.

System.out.println("You have " + i + " guess(es) left. Guess the number:"); – This line prints a message to the console that tells the user how many guesses they have left.

int guess = scanner.nextInt(); – This line reads an integer from the console and assigns it to the guess variable.

if (guess == randomNumber) { – This line begins an if statement that checks if the user’s guess is equal to the random number.

hasWon = true; – This line sets the hasWon variable to true.

break; – This line breaks out of the for loop.

} else if (guess < randomNumber) { – This line begins an else if statement that checks if the user’s guess is less than the random number.

System.out.println("Too low!"); – This line prints a message to the console.

} else { – This line begins an else statement that executes if none of the previous conditions are true.

System.out.println("Too high!"); – This line prints a message to the console.

} – This line ends the if statement.

} – This line ends the for loop.

if (hasWon) { – This line begins an if statement that checks if the user has won the game.

System.out.println("Congratulations, you guessed the number!"); – This line prints a message to the console.

} else { – This line begins an else statement that executes if the user has lost the game.

System.out.println("Game over. The number was " + randomNumber); – This line prints a message to the console that reveals the random number.

} – This line ends the if statement.

I hope that helps! Let me know if you have any further questions.

Join the conversation