forked from okaram/IntroJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFeb1.java
More file actions
61 lines (54 loc) · 1.18 KB
/
Copy pathFeb1.java
File metadata and controls
61 lines (54 loc) · 1.18 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
package okaram.samples;
import java.util.Scanner;
public class Feb1 {
public static int power(int base, int exponent)
{
if(exponent==0)
return 1;
else
return base * power(base, exponent-1);
}
public static void printCountdown(int n)
{
if(n<0)
return;
else {
System.out.println(n);
printCountdown(n-1);
}
}
public static void playGuessNumber(Scanner in, int n)
{
System.out.println("Guess my number");
int guess=in.nextInt();
if(n==guess) {
System.out.println("yay !!! :)");
} else {
System.out.println("nya nya nya nya nya");
playGuessNumber(in,n);
}
}
public static void guessNumber(Scanner in, int low, int high)
{
int guess=(low+high)/2;
System.out.println("Is it "+guess);
boolean isIt=in.nextBoolean();
if(isIt) {
System.out.println("I'm a genius !!!");
} else {
System.out.println("Is it too high?");
boolean tooHigh=in.nextBoolean();
if(tooHigh) {
guessNumber(in, low, guess-1);
} else {
guessNumber(in, guess+1,high);
}
}
}
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
//playGuessNumber(in, 7);
guessNumber(in, 1,100);
// playGuessNumber(in, 13);
}
}