-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberGame.java
More file actions
69 lines (59 loc) · 2.63 KB
/
NumberGame.java
File metadata and controls
69 lines (59 loc) · 2.63 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
61
62
63
64
65
66
67
68
69
/* Task 1 : Number Game */
/* Project Overveiw
1. Generate a random number within a specified range, such as 1 to 100.
2. Prompt the user to enter their guess for the generated number.
3. Compare the user's guess with the generated number and provide feedback on whether the guess
is correct, too high, or too low.
4. Repeat steps 2 and 3 until the user guesses the correct number.
5. Limit the number of attempts the user has to guess the number.
6. Add the option for multiple rounds, allowing the user to play again.
7. Display the user's score, which can be based on the number of attempts taken or rounds won.*/
import java.util.Random;
import java.util.Scanner;
public class NumberGame
{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Random random = new Random();
int lowerlimit = 1;
int upperlimit = 100;
int maxAttempts = 3;
int rounds = 0;
int totalAttempts = 0;
System.out.println("Welcome to the Number Guessing Game!");
boolean playAgain = true;
while (playAgain) {
int targetNumber = random.nextInt(upperlimit - lowerlimit + 1) + lowerlimit;
int attempts = 0;
boolean guessedCorrectly = false;
System.out.println("\nNew round! Guess the number between " + lowerlimit + " and " + upperlimit);
while (attempts < maxAttempts && !guessedCorrectly) {
System.out.print("Enter your guess: ");
int userGuess = scanner.nextInt();
attempts++;
if (userGuess == targetNumber)
{
System.out.println("Congratulations! You guessed the correct number in " + attempts + " attempts.");
guessedCorrectly = true;
}
else if (userGuess < targetNumber)
{
System.out.println("Too low! Try again.");
}
else
{
System.out.println("Too high! Try again.");
}
}
totalAttempts += attempts;
rounds++;
System.out.println("\nThe correct number was: " + targetNumber);
System.out.println("Round summary - Attempts: " + attempts);
System.out.print("\nDo you want to play again? (yes/no): ");
String playAgainResponse = scanner.next().toLowerCase();
playAgain = playAgainResponse.equals("yes");
}
System.out.println("\nGame Over! You played " + rounds + " rounds with a total of " + totalAttempts + " attempts.");
scanner.close();
}
}