-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberGame.java
More file actions
62 lines (45 loc) · 2.04 KB
/
NumberGame.java
File metadata and controls
62 lines (45 loc) · 2.04 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
# NumberGame.java
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 leastNum = 1;
int highestNum = 100;
int maxAttempts = 7;
int roundsPlayed = 0;
int roundsWon = 0;
boolean playAgain = true;
while(playAgain){
int Number = random.nextInt(highestNum - leastNum + 1) + leastNum;
System.out.println("Guess any number between " + leastNum + " and " + highestNum );
int attempts = 0;
boolean Correctguess = false;
while (attempts < maxAttempts) {
System.out.print("Enter your guess: ");
int guess = scanner.nextInt();
attempts++;
if (guess == Number) {
System.out.println("Congratulations. You have guessed the correct number " + Number + " in " + attempts + " attempts.");
roundsWon++;
Correctguess = true;
break;
} else if (guess < Number) {
System.out.println("The guess is too low. Try again.");
} else {
System.out.println("The guess is too high. Try again.");
}
}
roundsPlayed++;
if (!Correctguess) {
System.out.println("Sorry, you have reached the maximum number of attempts. The correct number was " + Number);
}
System.out.print("Do you want to play again?\n (yes/no): ");
String continuePlaying = scanner.next();
playAgain = continuePlaying.equalsIgnoreCase("yes");
}
System.out.println("\nYou played " + roundsPlayed + " rounds and your total score is " + roundsWon + ".");
scanner.close();
}
}