-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberGuessingGame.java
More file actions
63 lines (51 loc) · 2.43 KB
/
NumberGuessingGame.java
File metadata and controls
63 lines (51 loc) · 2.43 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
57
58
59
60
61
62
63
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();
int totalRounds = 0; // Total rounds played
int roundsWon = 0; // Rounds won by the player
boolean playAgain = true;
while (playAgain) {
int maxAttempts = 10; // Limit the number of attempts per round
int numberToGuess = random.nextInt(100) + 1; // Random number between 1 and 100
int attempts = 0; // Number of attempts in the current round
boolean guessedCorrectly = false;
System.out.println("A new round begins! Guess the number between 1 and 100.");
System.out.println("You have " + maxAttempts + " attempts.");
// Round logic
while (attempts < maxAttempts && !guessedCorrectly) {
attempts++;
System.out.print("Attempt " + attempts + ": Enter your guess: ");
int userGuess = scanner.nextInt();
if (userGuess == numberToGuess) {
guessedCorrectly = true;
System.out.println("Congratulations! You guessed the correct number.");
} else if (userGuess > numberToGuess) {
System.out.println("Too high! Try again.");
} else {
System.out.println("Too low! Try again.");
}
// If the player runs out of attempts
if (attempts == maxAttempts && !guessedCorrectly) {
System.out.println("Sorry, you've run out of attempts. The correct number was " + numberToGuess + ".");
}
}
// Update rounds won count
if (guessedCorrectly) {
roundsWon++;
}
totalRounds++;
// Ask if the user wants to play again
System.out.print("Do you want to play another round? (yes/no): ");
String response = scanner.next();
if (response.equalsIgnoreCase("no")) {
playAgain = false;
}
}
// Final score display
System.out.println("Game Over! You played " + totalRounds + " rounds and won " + roundsWon + " rounds.");
scanner.close();
}
}