-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGuess_game.java
More file actions
31 lines (27 loc) · 1.04 KB
/
Guess_game.java
File metadata and controls
31 lines (27 loc) · 1.04 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
import java.util.*;
public class Guess_game {
static String input(String prompt) {
System.out.print(prompt);
return new Scanner(System.in).nextLine();
}
public static void main(String[] args) {
System.out.println("Welcome! Let's play a guess game!");
Random random = new Random();
int randint = random.nextInt(100);
for (int i = 0; i < 5; i++) {
String input_num = input("Enter a number between 1 and 100: ");
int parsed_input = Integer.parseInt(input_num);
if (parsed_input == randint) {
System.out.println("You Got it!");
break;
} else if (parsed_input > randint) {
System.out.println("Think of a smaller number");
} else if (parsed_input < randint) {
System.out.println("Think of a larger number");
} else {
System.out.println("An error encounterd");
}
}
System.out.println("The number was: " + randint);
}
}