-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberGame.java
More file actions
49 lines (40 loc) · 1.77 KB
/
NumberGame.java
File metadata and controls
49 lines (40 loc) · 1.77 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
37
38
39
40
41
42
43
44
45
46
47
48
49
import java.util.Random;
import java.util.Scanner;
public class NumberGame {
public static void main(String[] args) {
System.out.println("Welcome to the Guess the Number Game!");
while (true) {
// Step 1: Generate a random number within the specified range
int secretNumber = new Random().nextInt(100) + 1;
// Additional details
int attempts = 0;
int maxAttempts = 10;
int roundsWon = 0;
Scanner sc = new Scanner(System.in);
while (attempts < maxAttempts) {
// Step 2: Prompt the user to enter their guess
System.out.print("\nGuess the number (between 1 and 100): ");
int userGuess = sc.nextInt();
// Step 3: Compare the user's guess and provide feedback
if (userGuess == secretNumber) {
System.out.println("Congratulations! You guessed the correct number " + secretNumber + " in "
+ (attempts + 1) + " attempts.");
roundsWon++;
break;
} else if (userGuess < secretNumber) {
System.out.println("Too low. Try again.");
} else {
System.out.println("Too high. Try again.");
}
attempts++;
}
// Check if the user wants to play again
System.out.print("\nDo you want to play again? (yes/no): ");
String playAgain = sc.next().toLowerCase();
if (!playAgain.equals("yes")) {
System.out.println("\nGame Over! You won " + roundsWon + " round(s).");
break;
}
}
}
}