Number Guessing Game Using Java With Source Code
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:
Find More Projects
Build a Quiz Game Using HTML CSS and JavaScript Introduction Hello coders, you might have played various games, but were you aware …
Emoji Catcher Game Using HTML CSS and JavaScript Introduction Hello Coders, Welcome to another new blog. In this article we’ve made a …
Typing Challenge Using HTML CSS and JavaScript Introduction Hello friends, all you developer friends are welcome to our new project. If you …
Breakout Game Using HTML CSS and JavaScript With Source Code Introduction Hello friends, welcome to today’s new blog post. All of you …
Digital and Analog Clock using HTML CSS and JavaScript Introduction : This project is a digital clock and stopwatch system, which allows …
Coffee Shop Website using HTML, CSS & JavaScript Introduction : This project is a website for coffee house business. It uses HTML …