forked from AllenDowney/ThinkJavaCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecursion.java
More file actions
71 lines (57 loc) · 1.45 KB
/
Recursion.java
File metadata and controls
71 lines (57 loc) · 1.45 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
62
63
64
65
66
67
68
69
70
71
public class Recursion {
public static void main(String[] args) {
System.out.println("countdown");
countdown(3);
System.out.println("countup");
countup(3);
System.out.println("newLine");
newLine();
System.out.println("nLines");
nLines(3);
System.out.println("threeLine");
threeLine();
System.out.println("displayBinary");
displayBinary(23);
System.out.println();
}
public static void countdown(int n) {
if (n == 0) {
System.out.println("Blastoff!");
} else {
System.out.println(n);
countdown(n - 1);
}
}
public static void newLine() {
System.out.println();
}
public static void threeLine() {
newLine();
newLine();
newLine();
}
public static void nLines(int n) {
if (n > 0) {
System.out.println();
nLines(n - 1);
}
}
public static void forever(String s) {
System.out.println(s);
forever(s);
}
public static void countup(int n) {
if (n == 0) {
System.out.println("Blastoff!");
} else {
countup(n - 1);
System.out.println(n);
}
}
public static void displayBinary(int value) {
if (value > 0) {
displayBinary(value / 2);
System.out.print(value % 2);
}
}
}