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:

Get Discount on Top Educational Courses

Brand NameDiscount InformationCoupon Codes Link
Educative.io20% discount on Educative courses and plans
W3Schools20% discount on W3Schools courses
KodeKloud10% discount on KodeKloud courses and plans
GeeksforGeeks30% discount on GeeksforGeeks courses
Target Test Prep20% discount on Target Test Prep
Coding Ninjas₹5000 discount on Coding Ninjas courses
Skillshare40% discount on Skillshare
DataCamp50% discount on DataCamp
365 Data Science57% discount on 365 Data Science Plans
Get SmarterFlat 20% discount on Get Smarter courses
SmartKeedaFlat 40% discount on SmartKeeda courses
StackSocial20% discount on StackSocial courses
				
					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

Auto Text Effect Animation Using Html CSS & JavaScript Introduction Hello friends, welcome to today’s new blog post. Today we have created …

Windows 12 Notepad Using Python Introduction: In this article, we will create a Windows 12 Notepad using Python. If you are a …

Animated Search Bar using Html CSS And JavaScript Introduction Hello friends, all of you developers are welcome to today’s beautiful blog post. …

Best Quiz Game Using HTML CSS And JavaScript Introduction Hello coders, welcome to another new blog. Today in this article we’ll learn …

Tower Blocks Game Using HTML CSS And JavaScript Introduction Hello coders, welcome to another new blog. Today in this blog we’ll learn …

Typing Speed Test Game using Python Introduction: In this article, we will create a Typing Speed Test Game using Python and Tkinter. …