Java Number Guessing Game Project: Two Solutions with Limited Guesses
Java Project - Number Guessing Game:
Number Guessing Game :
User guesses a randomly generated number within a range.
This game generates a random number, and the player attempts to guess it within a set range (e.g., 1–100). After each guess, the program tells the player if their guess is too high, too low, or correct.
Input: User's guess of a number within a range (e.g., 1–100).
Output: Indication of whether the guess is too high, too low, or correct.
Example:
- Input: 50 (Guess)
- Output: "Too low"
- Input: 75
- Output: "Correct!"
Here are two different solutions for the Number Guessing Game project in Java:
Solution 1: Using a Simple While Loop
Code:
import java.util.Scanner;
import java.util.Random;
public class NumberGuessingGameWhileLoop {
public static void main(String[] args) {
// Create a Scanner object to get user input
Scanner scanner = new Scanner(System.in);
// Create a Random object to generate a random number
Random random = new Random();
// Generate a random number between 1 and 100
int randomNumber = random.nextInt(100) + 1;
// Initialize variable for the user's guess
int guess = 0;
// Inform the user about the game
System.out.println("Welcome to the Number Guessing Game!");
System.out.println("Guess a number between 1 and 100.");
// Loop until the user guesses the correct number
while (guess != randomNumber) {
// Get the user's guess
System.out.print("Enter your guess: ");
guess = scanner.nextInt();
// Check if the guess is too high, too low, or correct
if (guess < randomNumber) {
System.out.println("Too low!");
} else if (guess > randomNumber) {
System.out.println("Too high!");
} else {
System.out.println("Correct! You've guessed the number.");
}
}
// Close the scanner to prevent resource leaks
scanner.close();
}
}
Output:
Welcome to the Number Guessing Game! Guess a number between 1 and 100. Enter your guess: 45 Too high! Enter your guess: 30 Too high! Enter your guess: 20 Too high! Enter your guess: 15 Too high! Enter your guess: 10 Too low! Enter your guess: 13 Correct! You've guessed the number.
Explanation :
- Random Number Generation: The Random object generates a random number between 1 and 100.
- Input: The user inputs their guesses using the Scanner object.
- Loop: The while loop continues until the user correctly guesses the number.
- Feedback: After each guess, the program checks whether the guess is too high, too low, or correct.
- End: The game ends when the correct number is guessed, and the loop terminates.
Solution 2: Using a For Loop with Guess Limits
Code:
import java.util.Scanner;
import java.util.Random;
public class NumberGuessingGameForLoop {
public static void main(String[] args) {
// Create a Scanner object to get user input
Scanner scanner = new Scanner(System.in);
// Create a Random object to generate a random number
Random random = new Random();
// Generate a random number between 1 and 100
int randomNumber = random.nextInt(100) + 1;
// Set a limit on the number of guesses
int maxAttempts = 10;
// Inform the user about the game and the number of attempts
System.out.println("Welcome to the Number Guessing Game!");
System.out.println("Guess a number between 1 and 100.");
System.out.println("You have " + maxAttempts + " attempts to guess the correct number.");
// Start the guessing process with a for loop that limits attempts
for (int attempts = 1; attempts <= maxAttempts; attempts++) {
// Get the user's guess
System.out.print("Attempt " + attempts + ": Enter your guess: ");
int guess = scanner.nextInt();
// Check if the guess is too high, too low, or correct
if (guess < randomNumber) {
System.out.println("Too low!");
} else if (guess > randomNumber) {
System.out.println("Too high!");
} else {
System.out.println("Correct! You've guessed the number in " + attempts + " attempts.");
return; // End the program after a correct guess
}
// Inform the user if they have exhausted their attempts
if (attempts == maxAttempts) {
System.out.println("Sorry, you've run out of attempts. The number was: " + randomNumber);
}
}
// Close the scanner to prevent resource leaks
scanner.close();
}
}
Output:
Welcome to the Number Guessing Game! Guess a number between 1 and 100. You have 10 attempts to guess the correct number. Attempt 1: Enter your guess: 50 Too high! Attempt 2: Enter your guess: 30 Too low! Attempt 3: Enter your guess: 40 Too low! Attempt 4: Enter your guess: 45 Too high! Attempt 5: Enter your guess: 42 Too low! Attempt 6: Enter your guess: 43 Too low! Attempt 7: Enter your guess: 44 Correct! You've guessed the number in 7 attempts.
Explanation:
- Random Number Generation: Similar to Solution 1, the Random object generates a random number between 1 and 100.
- Guess Limit: The game introduces a maximum number of guesses (10) using a for loop.
- Input: The user inputs their guesses through the Scanner object.
- Feedback: After each guess, the program informs the player if their guess is too high, too low, or correct.
- End: The game ends either when the correct number is guessed or when the maximum number of attempts is reached.
Java Code Editor:
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/projects/java/java-number-guessing-game-project.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics