-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGuessingGame.java
More file actions
73 lines (66 loc) · 1.9 KB
/
GuessingGame.java
File metadata and controls
73 lines (66 loc) · 1.9 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import java.util.Random;
import java.util.Scanner;
public class GuessingGame{
public static void main(String[] args){
Random randomGenerator = new Random();
Scanner input = new Scanner(System.in);
int randomInt = randomGenerator.nextInt(10);
int userInt = 0;
boolean loopBool = false;
while (!loopBool){
System.out.println("Please enter a valid numeric guess.");
while (!input.hasNextInt()){
System.out.println("You did not enter a valid number. Try again.");
input.next();
}
userInt = input.nextInt();
if (userInt == randomInt){
loopBool = true;
System.out.println("Your guess: " + userInt + "\nYour guess is correct!\n");
}
else if (userInt > randomInt){
System.out.println("Your guess: " + userInt + "\nYour guess was too high -- guess lower.\n");
}
else if (userInt < randomInt){
System.out.println("Your guess: " + userInt + "\nYour guess was too low -- guess higher.\n");
}
else{
System.out.println("Input leaked through.");
}
}
input.close();
}
}
/*
C:\Users\mguo1\Downloads>java GuessingGame
Please enter a valid numeric guess.
dsjk
You did not enter a valid number. Try again.
dsaj
You did not enter a valid number. Try again.
jlew
You did not enter a valid number. Try again.
1290
Your guess: 1290
Your guess was too high -- guess lower.
Please enter a valid numeric guess.
38
Your guess: 38
Your guess was too high -- guess lower.
Please enter a valid numeric guess.
27
Your guess: 27
Your guess was too high -- guess lower.
Please enter a valid numeric guess.
10
Your guess: 10
Your guess was too high -- guess lower.
Please enter a valid numeric guess.
9
Your guess: 9
Your guess was too high -- guess lower.
Please enter a valid numeric guess.
8
Your guess: 8
Your guess is correct!
*/