-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem2.java
More file actions
40 lines (37 loc) · 872 Bytes
/
Problem2.java
File metadata and controls
40 lines (37 loc) · 872 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
34
35
36
37
38
39
40
import java.util.Scanner;
public class Problem2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int ranNum = (int)(100 * Math.random()) + 1;
int count = 1;
int guess = 0;
int numMin = 1;
int numMax = 100;
Scanner in = new Scanner(System.in);
while(true) {
System.out.print("["+count+"] Guess a number (" + numMin + "-" + numMax + "): ");
guess = in.nextInt();
if (ranNum == guess) {
System.out.print("Correct! Number of guesses : " + count);
break;
}
else if(guess > numMax || guess < numMin){
System.out.println("Not in range!");
continue;
}
else {
if (ranNum > guess) {
System.out.println("Too small!");
count ++;
numMin = guess+1;
}
else {
System.out.println("Too large!");
count ++;
numMax = guess-1;
}
}
}
in.close();
}
}