-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberGuessingGame.java
More file actions
56 lines (46 loc) · 2.11 KB
/
NumberGuessingGame.java
File metadata and controls
56 lines (46 loc) · 2.11 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
50
51
52
53
54
55
56
import java.util.Random;
import java.util.Scanner;
public class NumberGuessingGame {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Random random = new Random();
boolean playAgain = true;
int totalScore = 0;
int roundNumber = 1;
while (playAgain) {
System.out.println("Round " + roundNumber);
System.out.println("I have generated a number between 1 and 100. Try to guess it!");
int randomNumber = random.nextInt(100) + 1;
int attempts = 0;
int maxAttempts = 5;
boolean guessedCorrectly = false;
while (attempts < maxAttempts && !guessedCorrectly) {
System.out.print("Enter your guess: ");
int userGuess = scanner.nextInt();
attempts++;
if (userGuess == randomNumber) {
guessedCorrectly = true;
System.out.println("Congratulations! You've guessed the correct number.");
totalScore += (maxAttempts - attempts + 1); // Score based on attempts left
} else if (userGuess < randomNumber) {
System.out.println("Too low! Try again.");
} else {
System.out.println("Too high! Try again.");
}
System.out.println("Attempts left: " + (maxAttempts - attempts));
}
if (!guessedCorrectly) {
System.out.println("Sorry! You've used all attempts. The correct number was " + randomNumber + ".");
}
System.out.println("Your current score: " + totalScore);
System.out.print("Do you want to play another round? (yes/no): ");
String userResponse = scanner.next();
if (!userResponse.equalsIgnoreCase("yes")) {
playAgain = false;
}
roundNumber++;
}
System.out.println("Thank you for playing! Your final score is: " + totalScore);
scanner.close();
}
}