-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask1.java
More file actions
86 lines (69 loc) · 3.23 KB
/
Copy pathtask1.java
File metadata and controls
86 lines (69 loc) · 3.23 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import java.util.Scanner;
import java.util.Random;
public class NumberGuessingGame {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Random random = new Random();
System.out.println("Welcome to the Number Guessing Game!");
System.out.println("I'm thinking of a number between 1 and 100.");
int totalRounds = 0;
int totalWins = 0;
int totalAttempts = 0;
boolean playAgain = true;
while (playAgain) {
// Game setup
int secretNumber = random.nextInt(100) + 1;
int attemptsLimit = 10;
int attemptsUsed = 0;
boolean roundWon = false;
totalRounds++;
System.out.printf("\n--- Round %d ---\n", totalRounds);
System.out.printf("You have %d attempts to guess the number.\n", attemptsLimit);
// Guessing loop
while (attemptsUsed < attemptsLimit && !roundWon) {
System.out.print("\nEnter your guess (1-100): ");
int guess;
try {
guess = scanner.nextInt();
if (guess < 1 || guess > 100) {
System.out.println("Please enter a number between 1 and 100.");
continue;
}
attemptsUsed++;
if (guess == secretNumber) {
roundWon = true;
totalWins++;
totalAttempts += attemptsUsed;
System.out.printf("Congratulations! You guessed the number in %d attempts!\n", attemptsUsed);
} else if (guess < secretNumber) {
System.out.println("Too low! Try again.");
} else {
System.out.println("Too high! Try again.");
}
System.out.printf("Attempts remaining: %d\n", attemptsLimit - attemptsUsed);
} catch (Exception e) {
System.out.println("Please enter a valid number.");
scanner.next(); // Clear the invalid input
}
}
if (!roundWon) {
System.out.printf("\nSorry, you've used all your attempts. The number was %d.\n", secretNumber);
}
// Ask to play again
System.out.print("\nWould you like to play again? (yes/no): ");
String playAgainInput = scanner.next().toLowerCase();
if (!playAgainInput.equals("yes")) {
playAgain = false;
}
}
// Display final statistics
System.out.println("\nGame Over!");
System.out.printf("Rounds played: %d\n", totalRounds);
System.out.printf("Rounds won: %d\n", totalWins);
if (totalWins > 0) {
double averageAttempts = (double) totalAttempts / totalWins;
System.out.printf("Average attempts per win: %.1f\n", averageAttempts);
}
scanner.close();
}
}