|
|
@@ -20,10 +20,15 @@ public class WriteLoops {
|
|
20
|
20
|
|
|
21
|
21
|
// Write a FOR loop that counts from 1 to 10.
|
|
22
|
22
|
// calling
|
|
23
|
|
- w = w + 1;
|
|
|
23
|
+ // w = w + 1;
|
|
24
|
24
|
// each time through the loop
|
|
25
|
25
|
|
|
26
|
26
|
// this will tell the test how many times the loop executed.
|
|
|
27
|
+
|
|
|
28
|
+ for(int i = 0; i < 5; i++) {
|
|
|
29
|
+ w += 1;
|
|
|
30
|
+ }
|
|
|
31
|
+
|
|
27
|
32
|
return w;
|
|
28
|
33
|
}
|
|
29
|
34
|
|
|
|
@@ -32,9 +37,13 @@ public class WriteLoops {
|
|
32
|
37
|
|
|
33
|
38
|
// Write a FOR loop that counts from 1 to 10.
|
|
34
|
39
|
// calling
|
|
35
|
|
- w = w + 1;
|
|
|
40
|
+ // w = w + 1;
|
|
36
|
41
|
// each time through the loop
|
|
37
|
42
|
|
|
|
43
|
+ for(int i = 0; i < 10; i++) {
|
|
|
44
|
+ w += 1;
|
|
|
45
|
+ }
|
|
|
46
|
+
|
|
38
|
47
|
return w;
|
|
39
|
48
|
}
|
|
40
|
49
|
|
|
|
@@ -43,9 +52,13 @@ public class WriteLoops {
|
|
43
|
52
|
|
|
44
|
53
|
// Write a FOR loop that makes 10 iterations, start at 21.
|
|
45
|
54
|
// calling
|
|
46
|
|
- w = w + 1;
|
|
|
55
|
+ // w = w + 1;
|
|
47
|
56
|
// each time through the loop
|
|
48
|
57
|
|
|
|
58
|
+ for(int i = 21; i <= 31; i++) {
|
|
|
59
|
+ w += 1;
|
|
|
60
|
+ }
|
|
|
61
|
+
|
|
49
|
62
|
return w;
|
|
50
|
63
|
}
|
|
51
|
64
|
|
|
|
@@ -54,8 +67,11 @@ public class WriteLoops {
|
|
54
|
67
|
|
|
55
|
68
|
// Write a FOR loop that counts down from 100 to 0.
|
|
56
|
69
|
// calling
|
|
57
|
|
- w = w + 1;
|
|
|
70
|
+ // w = w + 1;
|
|
58
|
71
|
// each time through the loop
|
|
|
72
|
+ for(int i = 100; i > 0; i--) {
|
|
|
73
|
+ w += 1;
|
|
|
74
|
+ }
|
|
59
|
75
|
|
|
60
|
76
|
return w;
|
|
61
|
77
|
}
|
|
|
@@ -65,8 +81,12 @@ public class WriteLoops {
|
|
65
|
81
|
|
|
66
|
82
|
// Write a FOR loop from 0 to 32 by 2s.
|
|
67
|
83
|
// calling
|
|
68
|
|
- w = w + 1;
|
|
|
84
|
+ // w = w + 1;
|
|
69
|
85
|
// each time through the loop
|
|
|
86
|
+ for(int i = 0; i < 32; i += 2) {
|
|
|
87
|
+ w += 1;
|
|
|
88
|
+ }
|
|
|
89
|
+
|
|
70
|
90
|
return w;
|
|
71
|
91
|
}
|
|
72
|
92
|
|
|
|
@@ -75,9 +95,13 @@ public class WriteLoops {
|
|
75
|
95
|
|
|
76
|
96
|
// Write a FOR loop from 1 to less than 5001 by 11s.
|
|
77
|
97
|
// calling
|
|
78
|
|
- w = w + 1;
|
|
|
98
|
+ // w = w + 1;
|
|
79
|
99
|
// each time through the loop
|
|
80
|
|
-
|
|
|
100
|
+
|
|
|
101
|
+ for(int i = 1; i < 5001; i += 11) {
|
|
|
102
|
+ w += 1;
|
|
|
103
|
+ }
|
|
|
104
|
+
|
|
81
|
105
|
return w;
|
|
82
|
106
|
}
|
|
83
|
107
|
|
|
|
@@ -87,8 +111,17 @@ public class WriteLoops {
|
|
87
|
111
|
// Write a nested FOR loop(s), where one counts from
|
|
88
|
112
|
// 0 to less than 20 and the inner one counts from 0 to 4
|
|
89
|
113
|
// calling
|
|
90
|
|
- w = w + 1;
|
|
|
114
|
+ // w = w + 1;
|
|
91
|
115
|
// each time through the inner loop
|
|
|
116
|
+
|
|
|
117
|
+
|
|
|
118
|
+
|
|
|
119
|
+ for(int i = 0; i < 20; i++) {
|
|
|
120
|
+ for(int j = 0; j <= 4; j++) {
|
|
|
121
|
+ w += 1;
|
|
|
122
|
+ }
|
|
|
123
|
+ }
|
|
|
124
|
+
|
|
92
|
125
|
|
|
93
|
126
|
return w;
|
|
94
|
127
|
}
|
|
|
@@ -102,9 +135,16 @@ public class WriteLoops {
|
|
102
|
135
|
// prints “Hello Zipcode” instead of the statement w = w + 1;
|
|
103
|
136
|
|
|
104
|
137
|
// calling
|
|
105
|
|
- w = w + 1;
|
|
|
138
|
+ // w = w + 1;
|
|
106
|
139
|
// each time through the inner loop
|
|
107
|
140
|
|
|
|
141
|
+
|
|
|
142
|
+ for(int i = 5; i < 105; i++) {
|
|
|
143
|
+ if(i <= 51) {
|
|
|
144
|
+ System.out.println("Hello Zipcode");
|
|
|
145
|
+ w += 1;
|
|
|
146
|
+ }
|
|
|
147
|
+ }
|
|
108
|
148
|
return w;
|
|
109
|
149
|
}
|
|
110
|
150
|
|
|
|
@@ -113,17 +153,19 @@ public class WriteLoops {
|
|
113
|
153
|
|
|
114
|
154
|
// sample while loop
|
|
115
|
155
|
while (i <= 5) {
|
|
116
|
|
- System.out.println("Eww.");
|
|
117
|
|
- i = i + 1;
|
|
|
156
|
+ System.out.println("Eww."); // 0, 1, 2, 3, 4, 5
|
|
|
157
|
+ i = i + 1; // 1, 2, 3, 4, 5, 6
|
|
118
|
158
|
}
|
|
119
|
159
|
|
|
120
|
160
|
// sample do...while loop
|
|
121
|
161
|
i = 8;
|
|
122
|
162
|
do {
|
|
123
|
|
- System.out.println("Eww.");
|
|
124
|
|
- i = i - 1;
|
|
|
163
|
+ System.out.println("Eww."); // 8, 7, 6, 5, 4, 3, 2, 1
|
|
|
164
|
+ i = i - 1;// 7, 6, 5, 4, 3, 2, 1, 0
|
|
125
|
165
|
} while (i > 0);
|
|
126
|
166
|
// what's the primary difference between them?!?
|
|
|
167
|
+ // The condition is checked at the begining for the while loop and at the end of the do while
|
|
|
168
|
+ // loop. This guarantees that the do while loop will be run at least once.
|
|
127
|
169
|
}
|
|
128
|
170
|
|
|
129
|
171
|
// Write a WHILE loop that checks “gpsCurrentLocation()”
|
|
|
@@ -135,9 +177,13 @@ public class WriteLoops {
|
|
135
|
177
|
// you need to use a .equals for two Strings.
|
|
136
|
178
|
|
|
137
|
179
|
// calling
|
|
138
|
|
- w = w + 1;
|
|
|
180
|
+ //w = w + 1;
|
|
139
|
181
|
// each time through the inner loop
|
|
140
|
|
-
|
|
|
182
|
+ while(!gpsCurrentLocation().equals("Home")) {
|
|
|
183
|
+ driveSomeMore();
|
|
|
184
|
+ w += 1;
|
|
|
185
|
+ }
|
|
|
186
|
+ System.out.println("Honey, I'm Home!");
|
|
141
|
187
|
|
|
142
|
188
|
return w;
|
|
143
|
189
|
}
|
|
|
@@ -157,9 +203,15 @@ public class WriteLoops {
|
|
157
|
203
|
// do your while loop here
|
|
158
|
204
|
|
|
159
|
205
|
// calling
|
|
160
|
|
- w = w + 1;
|
|
|
206
|
+ //w = w + 1;
|
|
161
|
207
|
// each time through the inner loop
|
|
162
|
208
|
|
|
|
209
|
+ while(runningScore < highestScore) {
|
|
|
210
|
+ runningScore += currentScore;
|
|
|
211
|
+ currentScore = gameNextScore();
|
|
|
212
|
+ w += 1;
|
|
|
213
|
+ }
|
|
|
214
|
+
|
|
163
|
215
|
return w; // >= 3;
|
|
164
|
216
|
}
|
|
165
|
217
|
|
|
|
@@ -174,9 +226,16 @@ public class WriteLoops {
|
|
174
|
226
|
// do your while loop here
|
|
175
|
227
|
|
|
176
|
228
|
// calling
|
|
177
|
|
- w = w + 1;
|
|
|
229
|
+ // w = w + 1;
|
|
178
|
230
|
// each time through the inner loop
|
|
179
|
|
-
|
|
|
231
|
+
|
|
|
232
|
+
|
|
|
233
|
+ do {
|
|
|
234
|
+ runningScore += currentScore;
|
|
|
235
|
+ currentScore = gameNextScore();
|
|
|
236
|
+ w += 1;
|
|
|
237
|
+ } while(runningScore < highestScore);
|
|
|
238
|
+ System.out.print("Number of times through the loop is " + w);
|
|
180
|
239
|
return w >= 3;
|
|
181
|
240
|
}
|
|
182
|
241
|
|
|
|
@@ -190,9 +249,16 @@ public class WriteLoops {
|
|
190
|
249
|
|
|
191
|
250
|
|
|
192
|
251
|
// calling
|
|
193
|
|
- w = w + 1;
|
|
|
252
|
+ // w = w + 1;
|
|
194
|
253
|
// each time through the inner loop
|
|
|
254
|
+ while(serverIsRunning()) {
|
|
|
255
|
+ waitFor(5);
|
|
|
256
|
+ w += 1;
|
|
|
257
|
+ }
|
|
195
|
258
|
|
|
|
259
|
+ if(!serverIsRunning()) {
|
|
|
260
|
+ tryServerRestart("Help!", adminPhoneNumber);
|
|
|
261
|
+ }
|
|
196
|
262
|
return w;
|
|
197
|
263
|
}
|
|
198
|
264
|
|
|
|
@@ -201,6 +267,7 @@ public class WriteLoops {
|
|
201
|
267
|
// and if it is, add 7 to “i”
|
|
202
|
268
|
public int loop50by7() {
|
|
203
|
269
|
int w = 0;
|
|
|
270
|
+ int i = 7;
|
|
204
|
271
|
|
|
205
|
272
|
|
|
206
|
273
|
// calling
|