-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGuessTheNumberGame.java
More file actions
49 lines (39 loc) · 1.83 KB
/
GuessTheNumberGame.java
File metadata and controls
49 lines (39 loc) · 1.83 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
import java.util.Scanner;
import java.util.Random;
public class GuessTheNumberGame {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Random random = new Random();
int rounds = 3; // Number of rounds
int maxAttempts = 5; // Maximum attempts per round
int score = 0;
System.out.println("Welcome to the Guess the Number Game!");
for (int round = 1; round <= rounds; round++) {
int randomNumber = random.nextInt(100) + 1; // Random number between 1 and 100
int attempts = 0;
boolean guessedCorrectly = false;
System.out.println("\nRound " + round + ": Guess the number between 1 and 100");
while (attempts < maxAttempts) {
System.out.print("Enter your guess: ");
int userGuess = scanner.nextInt();
attempts++;
if (userGuess == randomNumber) {
System.out.println("Congratulations! You've guessed the number.");
guessedCorrectly = true;
score += (maxAttempts - attempts + 1) * 10; // Points based on remaining attempts
break;
} else if (userGuess < randomNumber) {
System.out.println("The number is higher.");
} else {
System.out.println("The number is lower.");
}
System.out.println("Attempts left: " + (maxAttempts - attempts));
}
if (!guessedCorrectly) {
System.out.println("Sorry, you've used all attempts. The number was: " + randomNumber);
}
}
System.out.println("\nGame over! Your total score is: " + score);
scanner.close();
}
}