-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberGame.java
More file actions
60 lines (49 loc) · 2.17 KB
/
NumberGame.java
File metadata and controls
60 lines (49 loc) · 2.17 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
package Java_CodSoft;
import java.util.Random;
import java.util.Scanner;
public class NumberGame {
private static final int MAX_ATTEMPTS = 10;
private static final int LOWER_BOUND = 1;
private static final int UPPER_BOUND = 100;
public static void main(String args[]){
Scanner scanner = new Scanner(System.in);
boolean playAgain;
int totalScore = 0;
int round = 1;
do {
System.out.println("Round " + round);
int randomNumber = generateRandomNumber(LOWER_BOUND, UPPER_BOUND);
int attempts = 0;
boolean guessedCorrectly = false;
while (attempts < MAX_ATTEMPTS && !guessedCorrectly){
System.out.println("Enter your guess (between " + LOWER_BOUND + " and " + UPPER_BOUND + "): ");
int userGuess = scanner.nextInt();
attempts++;
if (userGuess == randomNumber){
System.out.println("Correct! You've guessed the number in " + attempts + " attempts. ");
guessedCorrectly = true;
totalScore += (MAX_ATTEMPTS - attempts + 1);
} else if (userGuess < randomNumber){
System.out.println("Too low! Try again.");
} else {
System.out.println("Too high! Try again.");
}
}
if (!guessedCorrectly){
System.out.println("Sorry! You've used all " + MAX_ATTEMPTS + " attempts. The correct number was " + randomNumber + ".");
}
System.out.println("Your total score is: " + totalScore);
System.out.println("Do you want to play another round? (yes/no): ");
String response = scanner.next();
playAgain = response.
equalsIgnoreCase("yes");
round++;
} while (playAgain);
System.out.println("Thank you for playing! Your final score is: " + totalScore);
scanner.close();
}
private static int generateRandomNumber(int lowerBound, int upperBound){
Random random = new Random();
return random.nextInt(upperBound - lowerBound + 1) + lowerBound;
}
}