-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGuessingGame.java
More file actions
33 lines (26 loc) · 1012 Bytes
/
Copy pathGuessingGame.java
File metadata and controls
33 lines (26 loc) · 1012 Bytes
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
import java.util.Random;
import java.util.Scanner;
public class GuessingGame{
public static void main(String[] args){
System.out.println("I’m thinking of a number between 1 and 100. Can you guess it?");
Random pick = new Random();
int t = pick.nextInt(100) + 1;
int numberOfTrials = 1;
while (true){
Scanner enter = new Scanner(System.in);
int guess = enter.nextInt();
if (guess != t){
if (guess < t){
System.out.println("Good try, but that’s too low. Try again.");
}
else if (guess > t){
System.out.println("Good try, but that’s too high. Try again.");
}
numberOfTrials += 1;
}
else{
System.out.println("Yes! You guessed correctly after " + numberOfTrials + " tries! Congratulations.");
}
}
}
}