forked from DhanushNehru/Hacktoberfest2025
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberGuessingGame.java
More file actions
36 lines (30 loc) · 1.38 KB
/
NumberGuessingGame.java
File metadata and controls
36 lines (30 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import java.util.Random;
import java.util.Scanner;
public class NumberGuessingGame {
public static void main(String[] args) {
// Create an instance of Random to generate a random number
Random random = new Random();
int randomNumber = random.nextInt(100) + 1; // Random number between 1 and 100
int guess = 0; // Initialize guess variable
int attempts = 0; // To track the number of attempts
// Create a Scanner object to read user input
Scanner scanner = new Scanner(System.in);
System.out.println("Welcome to the Number Guessing Game!");
System.out.println("I've picked a number between 1 and 100. Can you guess it?");
// Game loop: continues until the player guesses the correct number
while (guess != randomNumber) {
System.out.print("Enter your guess: ");
guess = scanner.nextInt(); // Get user's guess
attempts++; // Increment the attempt count
if (guess < randomNumber) {
System.out.println("Too low! Try again.");
} else if (guess > randomNumber) {
System.out.println("Too high! Try again.");
} else {
System.out.println("Congratulations! You guessed the correct number in " + attempts + " attempts.");
}
}
// Close the scanner
scanner.close();
}
}