-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathguess the number
30 lines (29 loc) · 895 Bytes
/
guess the number
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
import java.util.Scanner;
class Solution {
public static void solve(Scanner input, int a, int b) {
int m = (a + b) / 2;
System.out.println(m);
System.out.println("enter its \"TOO_SMALL or not or CORRECT\" guess ");
String s = input.next();
if (s.equals("CORRECT")) {
System.out.println(m+" is the number ");
return;
} else if (s.equals("TOO_SMALL")) {
solve(input, m + 1, b);
} else {
solve(input, a, m - 1);
}
}
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
System.out.println("Enter the number of test cases:");
int T = input.nextInt();
for (int ks = 1; ks <= T; ks++) {
System.out.println("Enter the lower limt :");
int a = input.nextInt();
System.out.println("Enter the upper limt:");
int b = input.nextInt();
solve(input, a + 1, b);
}
}
}