-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGuessNumberGame.java
35 lines (28 loc) · 1.07 KB
/
GuessNumberGame.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
33
34
35
import java.util.Scanner;
public class GuessNumberGame {
public static void main(String[] args) {
int randomNumber = (int)(Math.random() * 100) + 1;
boolean hasWon = false;
System.out.println("I have randomly chosen a number between 1 and 100.");
System.out.println("Try to guess it.");
Scanner scanner = new Scanner(System.in);
for(int i=10; i>0; i--){
System.out.println("You have " + i + " guess(es) left. Choose: ");
int guess = scanner.nextInt();
if(randomNumber < guess){
System.out.println("It's smaller than " + guess + ".");
} else if (randomNumber > guess) {
System.out.println("It's greater than " + guess + ".");
} else {
hasWon = true;
break;
}
}
if(hasWon){
System.out.println("Correct! You win in !");
} else {
System.out.println("Game over! You lose...");
System.out.println("The number was: " + randomNumber);
}
}
}