-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNumberGuessingGame.java
48 lines (40 loc) · 1.57 KB
/
NumberGuessingGame.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
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 minRange = 1;
int maxRange = 100;
int attempts = 0;
int score = 0;
System.out.println("Welcome to the Number Guessing Game!");
do {
int randomNumber = random.nextInt(maxRange - minRange + 1) + minRange;
int guess;
attempts = 0;
System.out.println("\nI've picked a number between " + minRange + " and " + maxRange + ". Guess it!");
while (true) {
System.out.print("Enter your guess: ");
guess = scanner.nextInt();
attempts++;
if (guess == randomNumber) {
System.out.println("Congratulations! You guessed it right in " + attempts + " attempts.");
score++;
break;
} else if (guess < randomNumber) {
System.out.println("Too low! Try again.");
} else {
System.out.println("Too high! Try again.");
}
}
System.out.print("Do you want to play again? (yes/no): ");
String playAgain = scanner.next();
if (!playAgain.equalsIgnoreCase("yes")) {
break;
}
} while (true);
System.out.println("Thanks for playing! Your final score is: " + score);
scanner.close();
}
}