Number Guessing Game Using Java With Source Code

Number Guessing Game Using Java

Introduction:

A number guessing game is a simple and fun way to learn about programming and the Java programming language. In this article, we will walk through the process of creating a number guessing game using Java.

Explanation:

 

The first step in creating a number guessing game is to generate a random number for the player to guess. In Java, this can be done using the Random class. The nextInt() method of this class generates a random integer between 0 and the specified maximum value. For our game, we want the number to be between 1 and 100, so we will use the following code to generate our random number:

 
Random rand = new Random();
int numberToGuess = rand.nextInt(100) + 1;

Next, we need to prompt the player to enter their guess. We will use the Scanner class to read the player’s input. The following code initializes the Scanner object and prompts the player to enter their guess:

Scanner scanner = new Scanner(System.in);
System.out.println("Guess a number between 1 and 100:");
int guess = scanner.nextInt();

Now that we have the player’s guess, we can compare it to the randomly generated number. If the player’s guess is too high, we will tell them to try again. If the player’s guess is too low, we will tell them to try again. If the player’s guess is correct, we will congratulate them and end the game.

 

Finally, we need to close the Scanner object to release any resources it is using. This can be done using the close() method

Source Code:

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

public class NumberGuessingGame {
    public static void main(String[] args) {
        Random rand = new Random();
        int numberToGuess = rand.nextInt(100) + 1;
        Scanner scanner = new Scanner(System.in);
        int guess;

        System.out.println("Welcome to the number guessing game!");
        System.out.println("Guess a number between 1 and 100:");

        while (true) {
            guess = scanner.nextInt();

            if (guess == numberToGuess) {
                System.out.println("Congratulations, you guessed the number!");
                break;
            } else if (guess < numberToGuess) {
                System.out.println("Your guess is too low. Try again:");
            } else {
                System.out.println("Your guess is too high. Try again:");
            }
        }

        scanner.close();
    }
}
				
			

This code generates a random number between 1 and 100 (inclusive) for the player to guess. The player is prompted to enter their guess, and the game will tell them if their guess is too high or too low. The game continues until the player correctly guesses the number, at which point they are congratulated and the game ends.

Output:

Guess the Number Using Java

Find More Projects

Resume Builder Application using Java With Source Code Graphical User Interface [GUI] Introduction: The Resume Builder Application is a powerful and user-friendly …

Encryption Tool using java with complete source Code GUI Introduction: The Encryption Tool is a Java-based GUI application designed to help users …

Movie Ticket Booking System using Java With Source Code Graphical User Interface [GUI] Introduction: The Movie Ticket Booking System is a Java …

Video Call Website Using HTML, CSS, and JavaScript (Source Code) Introduction Hello friends, welcome to today’s new blog post. Today we have …

promise day using html CSS and JavaScript Introduction Hello all my developers friends my name is Gautam and everyone is welcome to …

Age Calculator Using HTML, CSS, and JavaScript Introduction Hello friends, my name is Gautam and you are all welcome to today’s new …