|
4 | 4 | * Examples from Chapter 5. |
5 | 5 | */ |
6 | 6 | public class Conditional { |
7 | | - |
| 7 | + |
8 | 8 | public static void main(String[] args) { |
9 | 9 | String fruit1 = "Apple"; |
10 | 10 | String fruit2 = "Orange"; |
11 | 11 | System.out.println(fruit1.equals(fruit2)); |
12 | | - |
| 12 | + |
13 | 13 | int x = 17; |
14 | 14 | int n = 18; |
15 | | - |
| 15 | + |
16 | 16 | if (x > 0) { |
17 | 17 | System.out.println("x is positive"); |
18 | 18 | } |
19 | | - |
| 19 | + |
20 | 20 | if (x % 2 == 0) { |
21 | 21 | System.out.println("x is even"); |
22 | 22 | } else { |
23 | 23 | System.out.println("x is odd"); |
24 | 24 | } |
25 | | - |
| 25 | + |
26 | 26 | if (x > 0) { |
27 | 27 | System.out.println("x is positive"); |
28 | 28 | } else if (x < 0) { |
29 | 29 | System.out.println("x is negative"); |
30 | 30 | } else { |
31 | 31 | System.out.println("x is zero"); |
32 | 32 | } |
33 | | - |
| 33 | + |
34 | 34 | if (x == 0) { |
35 | 35 | System.out.println("x is zero"); |
36 | 36 | } else { |
@@ -40,118 +40,17 @@ public static void main(String[] args) { |
40 | 40 | System.out.println("x is negative"); |
41 | 41 | } |
42 | 42 | } |
| 43 | + |
43 | 44 | boolean evenFlag = (n % 2 == 0); // true if n is even |
44 | 45 | boolean positiveFlag = (x > 0); // true if x is positive |
45 | | - |
| 46 | + |
46 | 47 | if (evenFlag) { |
47 | 48 | System.out.println("n was even when I checked it"); |
48 | 49 | } |
49 | | - |
| 50 | + |
50 | 51 | if (!evenFlag) { |
51 | 52 | System.out.println("n was odd when I checked it"); |
52 | 53 | } |
53 | | - |
54 | | - System.out.println("printLogarithm"); |
55 | | - printLogarithm(3.0); |
56 | | - |
57 | | - System.out.println("countdown"); |
58 | | - countdown(3); |
59 | | - |
60 | | - System.out.println("countup"); |
61 | | - countup(3); |
62 | | - |
63 | | - System.out.println("newLine"); |
64 | | - newLine(); |
65 | | - |
66 | | - System.out.println("nLines"); |
67 | | - nLines(3); |
68 | | - |
69 | | - System.out.println("threeLine"); |
70 | | - threeLine(); |
71 | | - |
72 | | - System.out.println("displayBinary"); |
73 | | - displayBinary(23); |
74 | | - System.out.println(); |
75 | | - |
76 | | - System.out.println("scandouble"); |
77 | | - scanDouble(); |
78 | | - |
79 | | - System.out.println("scandouble2"); |
80 | | - scanDouble2(); |
81 | | - } |
82 | | - |
83 | | - public static void printLogarithm(double x) { |
84 | | - if (x <= 0.0) { |
85 | | - System.err.println("Error: x must be positive."); |
86 | | - return; |
87 | | - } |
88 | | - double result = Math.log(x); |
89 | | - System.out.println("The log of x is " + result); |
90 | | - } |
91 | | - |
92 | | - public static void scanDouble() { |
93 | | - Scanner in = new Scanner(System.in); |
94 | | - System.out.print("Enter a number: "); |
95 | | - double x = in.nextDouble(); |
96 | | - printLogarithm(x); |
97 | | - } |
98 | | - |
99 | | - public static void scanDouble2() { |
100 | | - Scanner in = new Scanner(System.in); |
101 | | - System.out.print("Enter a number: "); |
102 | | - if (!in.hasNextDouble()) { |
103 | | - String word = in.next(); |
104 | | - System.err.println(word + " is not a number"); |
105 | | - return; |
106 | | - } |
107 | | - double x = in.nextDouble(); |
108 | | - printLogarithm(x); |
109 | | - } |
110 | | - |
111 | | - public static void countdown(int n) { |
112 | | - if (n == 0) { |
113 | | - System.out.println("Blastoff!"); |
114 | | - } else { |
115 | | - System.out.println(n); |
116 | | - countdown(n - 1); |
117 | | - } |
118 | | - } |
119 | | - |
120 | | - public static void newLine() { |
121 | | - System.out.println(); |
122 | | - } |
123 | | - |
124 | | - public static void threeLine() { |
125 | | - newLine(); |
126 | | - newLine(); |
127 | | - newLine(); |
128 | | - } |
129 | | - |
130 | | - public static void nLines(int n) { |
131 | | - if (n > 0) { |
132 | | - System.out.println(); |
133 | | - nLines(n - 1); |
134 | | - } |
135 | | - } |
136 | | - |
137 | | - public static void forever(String s) { |
138 | | - System.out.println(s); |
139 | | - forever(s); |
140 | | - } |
141 | | - |
142 | | - public static void countup(int n) { |
143 | | - if (n == 0) { |
144 | | - System.out.println("Blastoff!"); |
145 | | - } else { |
146 | | - countup(n - 1); |
147 | | - System.out.println(n); |
148 | | - } |
149 | | - } |
150 | | - |
151 | | - public static void displayBinary(int value) { |
152 | | - if (value > 0) { |
153 | | - displayBinary(value / 2); |
154 | | - System.out.print(value % 2); |
155 | | - } |
156 | 54 | } |
| 55 | + |
157 | 56 | } |