-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGuessNumber.java
35 lines (25 loc) · 928 Bytes
/
GuessNumber.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.Random;
import java.util.Scanner;
public class GuessNumber {
public static void main(String[] args){
// 1. Generate a random number between 0-100:
Random r = new Random();
int random = r.nextInt(101);
Scanner s = new Scanner(System.in);
int guess = -1;
System.out.println("Guess a number between 0 and 100");
while(guess != random) {
// 2. Get the guess from the user:
System.out.println("Enter your guess");
guess = s.nextInt();
// 3. Check if the guess is too low or too high
if (guess < random) {
System.out.println("Too low!");
}
else if (guess > random) {
System.out.println("Too high!");
}
}
System.out.println("Correct guess, number is:" + random);
}
}