-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGuess.java
30 lines (25 loc) · 809 Bytes
/
Guess.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
import java.util.Scanner;
import java.util.Random;
public class Guess {
public static void main (String[] args) {
Random random = new Random();
int secretNum = random.nextInt(10) + 1;
int numGuesses = 0;
int guess = 0;
Scanner scanner = new Scanner(System.in);
while (guess != secretNum) {
System.out.print("Enter your guess: ");
guess = scanner.nextInt();
numGuesses++;
if (guess < secretNum) {
System.out.println("Your guess is too low.");
} else if (guess > secretNum) {
System.out.println("Your guess is too high.");
} else {
System.out.println("You got it!");
}
}
System.out.println("It took you" + numGuesses + " guesses, ");
scanner.close();
}
}