Number Guessing Game in Java

In this post, you will learn how to write a program for Number Guessing Game in Java. The Number Guessing Game in Java is a simple text-based game where the computer randomly selects a secret number within a specified range, and the player’s objective is to guess the number correctly.

The game provides feedback to the player after each guess, indicating whether the guessed number is higher, lower, or equal to the secret number. The game continues until the player correctly guesses the secret number.

Program building steps for Number Guessing Game

Let’s go through the Number Guessing Game program step by step:

  • The program starts by importing the required classes: java.util.Scanner and java.util.Random. These classes will allow us to read input from the user and generate random numbers, respectively.
  • Inside the main method, the program creates instances of Scanner and Random classes for reading user input and generating random numbers.
  • The program sets the range of numbers for the secret number using variables minNumber and maxNumber. In this case, the range is set from 1 to 100.
  • The program generates a random secret number using the nextInt method of the Random class. It calculates a random number within the range (maxNumber - minNumber + 1) and adds minNumber to ensure the secret number falls within the specified range.
  • The program initializes variables attempts and guess to keep track of the number of attempts made by the player and the current guess entered by the player.
  • The program displays a welcome message, introducing the player to the Number Guessing Game and instructing them to guess the secret number within the given range.
  • The game loop starts using a while loop, which will continue until the player correctly guesses the secret number.
  • Inside the loop, the program prompts the player to enter their guess by displaying the message “Enter your guess: “.
  • The program reads the player’s input using the nextInt method of the Scanner class and stores it in the variable guess.
  • The program increments the attempts variable to keep track of the number of attempts made by the player.
  • The program checks if the player’s guess is equal to the secret number using an if statement.
  • If the guess is correct, the program prints a congratulatory message, displaying the secret number, and the number of attempts made, and breaks out of the loop.
  • If the guess is incorrect, the program checks whether the guess is lower or higher than the secret number using if and else if statements.
  • If the guess is lower than the secret number, the program prints “Too low! Try again.” and prompts the player for another guess.
  • If the guess is higher than the secret number, the program prints “Too high! Try again.” and prompts the player for another guess.
  • The game loop continues until the player correctly guesses the secret number, at which point the loop is terminated by the break statement.
  • After the loop, the Scanner is closed to release system resources.

Java Program for Number Guessing Game

package com.javacodepoint.game;

import java.util.Random;
import java.util.Scanner;

public class NumberGuessingGame {
	
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);

		Random random = new Random();
		int secretNumber = random.nextInt(100) + 1;
		int attempts = 0;
		int guess;

		System.out.println("Welcome to the Number Guessing Game!");
		System.out.println("Try to guess the secret number between 1 and 100.");

		while (true) {
			System.out.print("Enter your guess: ");
			guess = scanner.nextInt();
			attempts++;

			if (guess == secretNumber) {
				System.out.println(
						"Congratulations! You guessed the number " + secretNumber + " in " + attempts + " attempts!");
				break;
			} else if (guess < secretNumber) {
				System.out.println("Too low! Try again.");
			} else {
				System.out.println("Too high! Try again.");
			}
		}

		scanner.close();
	}
}

OUTPUT:

Welcome to the Number Guessing Game!
Try to guess the secret number between 1 and 100.
Enter your guess: 70
Too high! Try again.
Enter your guess: 35
Too high! Try again.
Enter your guess: 20
Too low! Try again.
Enter your guess: 25
Too low! Try again.
Enter your guess: 30
Too high! Try again.
Enter your guess: 27
Too low! Try again.
Enter your guess: 28
Congratulations! You guessed the number 28 in 7 attempts!

See also: Hangman Game | Tic-Tac-Toe Game | Rock Paper Scissors Game

Java logical programs list


Java Basic Programs

Java Programs based on the Collection Framework

Leave a Reply

Your email address will not be published. Required fields are marked *