-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGuessTheNumber.java
More file actions
52 lines (44 loc) · 2.03 KB
/
Copy pathGuessTheNumber.java
File metadata and controls
52 lines (44 loc) · 2.03 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
import java.util.Scanner;
import java.util.Random;
public class GuessTheNumber {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Random random = new Random();
int minRange = 1;
int maxRange = 100;
int maxAttempts = 5;
int score = 0;
System.out.println("Welcome to the Guess the Number game!");
boolean playAgain = true;
while (playAgain) {
int numberToGuess = random.nextInt(maxRange - minRange + 1) + minRange;
System.out.println("A random number between " + minRange + " and " + maxRange + " has been generated.");
int attempts = 0;
boolean guessed = false;
while (!guessed && attempts < maxAttempts) {
System.out.print("Guess the number: ");
int guess = scanner.nextInt();
attempts++;
if (guess == numberToGuess) {
System.out.println("Congratulations! You guessed the correct number in " + attempts + " attempts.");
guessed = true;
score += maxAttempts - attempts + 1; // Calculate score
} else if (guess < numberToGuess) {
System.out.println("Too low! Try again.");
} else {
System.out.println("Too high! Try again.");
}
}
if (!guessed) {
System.out.println("Sorry, you didn't guess the number. The correct number was: " + numberToGuess);
}
System.out.print("Do you want to play again? (yes/no): ");
String playAgainInput = scanner.next().toLowerCase();
if (!playAgainInput.equals("yes")) {
playAgain = false;
System.out.println("Thanks for playing! Your total score is: " + score);
}
}
scanner.close();
}
}