-
Notifications
You must be signed in to change notification settings - Fork 0
/
GuessingGame.java
32 lines (26 loc) · 976 Bytes
/
GuessingGame.java
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
import java.util.Random;
import java.util.Scanner;
public class GuessingGame {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Random random = new Random();
System.out.println("Guess a number between 1 to 10, You'll get only 3 Chances to Guess");
int secretNumber = random.nextInt(10) + 1; // 1 to 10
boolean guessLimit = true;
int guessCount = 0;
while (guessLimit) {
System.out.print("Guess-> ");
int userNumber = scanner.nextInt();
if (userNumber == secretNumber) {
System.out.println("You Win!!!!!!!!!!");
guessLimit = false;
} else {
guessCount++;
if (guessCount == 3)
guessLimit = false;
}
}
if (guessCount == 3)
System.out.println("You Lose......\nThe Secret number was: "+secretNumber);
}
}