-
Notifications
You must be signed in to change notification settings - Fork 0
/
NumberGame.java
54 lines (48 loc) · 1.7 KB
/
NumberGame.java
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
import java.util.Scanner;
public class NumberGame {
private static final int MIN_RANGE = 1;
private static final int MAX_RANGE = 100;
private static final int MAX_ATTEMPTS = 5;
private int secretNumber;
private int attempts;
private Scanner scanner;
public NumberGame() {
scanner = new Scanner(System.in);
}
public void start() {
boolean playAgain = true;
while (playAgain) {
playRound();
System.out.println("Do you want to play again? (yes/no)");
String choice = scanner.next();
playAgain = choice.equalsIgnoreCase("yes");
}
scanner.close();
}
private void playRound() {
System.out.println("Welcome to the Number Game!");
secretNumber = generateRandomNumber(MIN_RANGE, MAX_RANGE);
attempts = 0;
int guess;
while (true) {
System.out.print("Enter your guess: ");
guess = scanner.nextInt();
attempts++;
if (guess == secretNumber) {
System.out.println("Congratulations! You guessed the number in " + attempts + " attempts.");
break;
} else if (guess < secretNumber) {
System.out.println("Too low! Try again.");
} else {
System.out.println("Too high! Try again.");
}
if (attempts >= MAX_ATTEMPTS) {
System.out.println("Sorry, you've reached the maximum number of attempts. The number was: " + secretNumber);
break;
}
}
}
private int generateRandomNumber(int min, int max) {
return (int) (Math.random() * (max - min + 1) + min);
}
}