-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogramone.java
More file actions
58 lines (48 loc) · 1.95 KB
/
programone.java
File metadata and controls
58 lines (48 loc) · 1.95 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
import java.util.Random;
import java.util.Scanner;
public class programone {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Random random = new Random();
int Max_try = 10;
System.out.println("Welcome to the the \" Number-Guess \" game.");
System.out.println("Guess the number between " + 1 + " to " + 100);
boolean play = true;
do {
int secretNumber = random.nextInt(100) + 1;
int tried = 0;
int gs;
while (true) {
if (tried == Max_try) {
System.out.println("Sorry, you've reached the maximum number of attempts. The correct number was: "
+ secretNumber);
break;
} else {
System.out.print("Enter your guess: ");
gs = sc.nextInt();
tried++;
if (gs == secretNumber) {
System.out
.println("Congratulations! You guessed the correct number in " + tried + " attempts.");
System.out.printf("Your Score is : %d. \n \n ", (1000 - tried * 100));
break;
} else if (gs < secretNumber) {
System.out.println("Too low. Try again.");
} else {
System.out.println("Too high. Try again.");
}
}
}
// rounds++;
System.out.print("Do you want to play another round? (Y/N): ");
String playAgain = sc.next();
if (playAgain.equalsIgnoreCase("Y")) {
play = true;
} else {
System.out.println("Ending the Game.\n");
break;
}
} while (play);
sc.close();
}
}