-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberGuessGame.java
More file actions
38 lines (29 loc) 路 1019 Bytes
/
Copy pathNumberGuessGame.java
File metadata and controls
38 lines (29 loc) 路 1019 Bytes
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
37
38
import java.util.Scanner;
import java.util.Random;
public class NumberGuessGame {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Random rand = new Random();
int randomNumber = rand.nextInt(100) + 1; // 1 to 100
int guess;
int attempts = 0;
System.out.println("=== Number Guessing Game ===");
System.out.println("Guess a number between 1 and 100");
do {
System.out.print("Enter your guess: ");
guess = sc.nextInt();
attempts++;
if (guess > randomNumber) {
System.out.println("Too High!");
}
else if (guess < randomNumber) {
System.out.println("Too Low!");
}
else {
System.out.println("Correct! 馃帀");
System.out.println("You guessed in " + attempts + " attempts.");
}
} while (guess != randomNumber);
sc.close();
}
}