|
@@ -20,7 +20,9 @@ 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
|
+ for(int i=1; i<=5; i++){
|
|
24
|
+ w = w + 1;
|
|
25
|
+ }
|
24
|
26
|
// each time through the loop
|
25
|
27
|
|
26
|
28
|
// this will tell the test how many times the loop executed.
|
|
@@ -32,7 +34,9 @@ public class WriteLoops {
|
32
|
34
|
|
33
|
35
|
// Write a FOR loop that counts from 1 to 10.
|
34
|
36
|
// calling
|
35
|
|
- w = w + 1;
|
|
37
|
+ for(int i=1; i<=10; i++){
|
|
38
|
+ w = w + 1;
|
|
39
|
+ }
|
36
|
40
|
// each time through the loop
|
37
|
41
|
|
38
|
42
|
return w;
|
|
@@ -43,7 +47,9 @@ public class WriteLoops {
|
43
|
47
|
|
44
|
48
|
// Write a FOR loop that makes 10 iterations, start at 21.
|
45
|
49
|
// calling
|
46
|
|
- w = w + 1;
|
|
50
|
+ for(int i=21; i<=31; i++){
|
|
51
|
+ w = w + 1;
|
|
52
|
+ }
|
47
|
53
|
// each time through the loop
|
48
|
54
|
|
49
|
55
|
return w;
|
|
@@ -54,7 +60,9 @@ public class WriteLoops {
|
54
|
60
|
|
55
|
61
|
// Write a FOR loop that counts down from 100 to 0.
|
56
|
62
|
// calling
|
57
|
|
- w = w + 1;
|
|
63
|
+ for(int i=100; i>0; i--){
|
|
64
|
+ w = w + 1;
|
|
65
|
+ }
|
58
|
66
|
// each time through the loop
|
59
|
67
|
|
60
|
68
|
return w;
|
|
@@ -65,7 +73,9 @@ public class WriteLoops {
|
65
|
73
|
|
66
|
74
|
// Write a FOR loop from 0 to 32 by 2s.
|
67
|
75
|
// calling
|
68
|
|
- w = w + 1;
|
|
76
|
+ for(int i=0; i<32; i+=2){
|
|
77
|
+ w = w + 0;
|
|
78
|
+ }
|
69
|
79
|
// each time through the loop
|
70
|
80
|
return w;
|
71
|
81
|
}
|
|
@@ -75,7 +85,9 @@ public class WriteLoops {
|
75
|
85
|
|
76
|
86
|
// Write a FOR loop from 1 to less than 5001 by 11s.
|
77
|
87
|
// calling
|
78
|
|
- w = w + 1;
|
|
88
|
+ for(int i=1; i<5001; i+=11){
|
|
89
|
+ w = w + 1;
|
|
90
|
+ }
|
79
|
91
|
// each time through the loop
|
80
|
92
|
|
81
|
93
|
return w;
|
|
@@ -87,7 +99,11 @@ public class WriteLoops {
|
87
|
99
|
// Write a nested FOR loop(s), where one counts from
|
88
|
100
|
// 0 to less than 20 and the inner one counts from 0 to 4
|
89
|
101
|
// calling
|
90
|
|
- w = w + 1;
|
|
102
|
+ for(int i=0; i<20; i++){
|
|
103
|
+ for(int j=0; j<=4; j++){
|
|
104
|
+ w = w + 1;
|
|
105
|
+ }
|
|
106
|
+ }
|
91
|
107
|
// each time through the inner loop
|
92
|
108
|
|
93
|
109
|
return w;
|
|
@@ -102,7 +118,13 @@ public class WriteLoops {
|
102
|
118
|
// prints “Hello Zipcode” instead of the statement w = w + 1;
|
103
|
119
|
|
104
|
120
|
// calling
|
105
|
|
- w = w + 1;
|
|
121
|
+ for(int i=5; i<=105; i++){
|
|
122
|
+ if(i>51){
|
|
123
|
+ System.out.println("Hello zipcode");
|
|
124
|
+ } else{
|
|
125
|
+ w = w + 1;
|
|
126
|
+ }
|
|
127
|
+ }
|
106
|
128
|
// each time through the inner loop
|
107
|
129
|
|
108
|
130
|
return w;
|
|
@@ -135,8 +157,12 @@ public class WriteLoops {
|
135
|
157
|
// you need to use a .equals for two Strings.
|
136
|
158
|
|
137
|
159
|
// calling
|
138
|
|
- w = w + 1;
|
|
160
|
+ while(!gpsCurrentLocation().equals("Home")){
|
|
161
|
+ w = w + 1;
|
|
162
|
+ driveSomeMore();
|
|
163
|
+ }
|
139
|
164
|
// each time through the inner loop
|
|
165
|
+ System.out.println("Honey, I'm home");
|
140
|
166
|
|
141
|
167
|
|
142
|
168
|
return w;
|
|
@@ -156,9 +182,11 @@ public class WriteLoops {
|
156
|
182
|
|
157
|
183
|
// do your while loop here
|
158
|
184
|
|
159
|
|
- // calling
|
160
|
|
- w = w + 1;
|
161
|
|
- // each time through the inner loop
|
|
185
|
+ while(runningScore < highestScore){
|
|
186
|
+ runningScore += currentScore;
|
|
187
|
+ currentScore = gameNextScore();
|
|
188
|
+ w = w + 1;
|
|
189
|
+ }
|
162
|
190
|
|
163
|
191
|
return w; // >= 3;
|
164
|
192
|
}
|
|
@@ -173,9 +201,11 @@ public class WriteLoops {
|
173
|
201
|
|
174
|
202
|
// do your while loop here
|
175
|
203
|
|
176
|
|
- // calling
|
177
|
|
- w = w + 1;
|
178
|
|
- // each time through the inner loop
|
|
204
|
+ do{
|
|
205
|
+ runningScore += currentScore;
|
|
206
|
+ currentScore = gameNextScore();
|
|
207
|
+ w = w + 1;
|
|
208
|
+ }while(runningScore < highestScore);
|
179
|
209
|
|
180
|
210
|
return w >= 3;
|
181
|
211
|
}
|
|
@@ -189,9 +219,15 @@ public class WriteLoops {
|
189
|
219
|
String adminPhoneNumber = "+1 202 456 1111";
|
190
|
220
|
|
191
|
221
|
|
192
|
|
- // calling
|
193
|
|
- w = w + 1;
|
194
|
|
- // each time through the inner loop
|
|
222
|
+ while(serverIsRunning()){
|
|
223
|
+ waitFor(5);
|
|
224
|
+ w = w + 1;
|
|
225
|
+ // each time through the inner loop
|
|
226
|
+ }
|
|
227
|
+ if(!serverIsRunning()){
|
|
228
|
+ sendEmergencyText("Help!", adminPhoneNumber);
|
|
229
|
+ tryServerRestart("Help!", adminPhoneNumber);
|
|
230
|
+ }
|
195
|
231
|
|
196
|
232
|
return w;
|
197
|
233
|
}
|
|
@@ -201,12 +237,14 @@ public class WriteLoops {
|
201
|
237
|
// and if it is, add 7 to “i”
|
202
|
238
|
public int loop50by7() {
|
203
|
239
|
int w = 0;
|
204
|
|
-
|
205
|
|
-
|
|
240
|
+ int i = 7;
|
|
241
|
+
|
|
242
|
+ while(i<50){
|
|
243
|
+ i += 7;
|
206
|
244
|
// calling
|
207
|
245
|
w = w + 1;
|
208
|
246
|
// each time through the inner loop
|
209
|
|
-
|
|
247
|
+ }
|
210
|
248
|
return w;
|
211
|
249
|
}
|
212
|
250
|
|
|
@@ -240,9 +278,12 @@ public class WriteLoops {
|
240
|
278
|
int sumOfThrees = 0;
|
241
|
279
|
|
242
|
280
|
|
|
281
|
+ for (int i=0; i<threes_array.length; i++) {
|
|
282
|
+ sumOfThrees = sumOfThrees + threes_array[i];
|
243
|
283
|
// calling
|
244
|
284
|
w = w + 1;
|
245
|
285
|
// each time through the inner loop
|
|
286
|
+ }
|
246
|
287
|
|
247
|
288
|
System.out.print("The Sum is ");
|
248
|
289
|
System.out.println(sumOfThrees);
|
|
@@ -255,11 +296,19 @@ public class WriteLoops {
|
255
|
296
|
public int rewriteFooAsWhile() {
|
256
|
297
|
int w = 0;
|
257
|
298
|
int sumOfThrees = 0;
|
258
|
|
-
|
259
|
|
-
|
260
|
|
- // calling
|
261
|
|
- w = w + 1;
|
262
|
|
- // each time through the inner loop
|
|
299
|
+ int i = 0;
|
|
300
|
+
|
|
301
|
+ while(true){
|
|
302
|
+ if(i < threes_array.length){
|
|
303
|
+ sumOfThrees = sumOfThrees + threes_array[i];
|
|
304
|
+ // calling
|
|
305
|
+ w = w + 1;
|
|
306
|
+ // each time through the inner loop
|
|
307
|
+ }
|
|
308
|
+ else{
|
|
309
|
+ break;
|
|
310
|
+ }
|
|
311
|
+ }
|
263
|
312
|
|
264
|
313
|
System.out.print("The Sum is ");
|
265
|
314
|
System.out.println(sumOfThrees);
|
|
@@ -277,12 +326,19 @@ public class WriteLoops {
|
277
|
326
|
public int manageYardAndJunior() {
|
278
|
327
|
int w = 0;
|
279
|
328
|
boolean onTime = true;
|
280
|
|
-
|
|
329
|
+
|
281
|
330
|
// ADD YOUR CODE here.
|
282
|
|
-
|
283
|
|
- // be sure to call
|
284
|
|
- w = w + 1;
|
285
|
|
- // each time inside the loop
|
|
331
|
+ boolean yardNeedsMowed = true;
|
|
332
|
+
|
|
333
|
+ while(isSummer()){
|
|
334
|
+ if(yardNeedsMowed){
|
|
335
|
+ yellAtJuniorToMowLawn();
|
|
336
|
+ // be sure to call
|
|
337
|
+ w = w + 1;
|
|
338
|
+ // each time inside the loop
|
|
339
|
+ }
|
|
340
|
+ }
|
|
341
|
+ sendJuniorBackToSchool("First Day of School");
|
286
|
342
|
|
287
|
343
|
return w;
|
288
|
344
|
}
|
|
@@ -296,10 +352,12 @@ public class WriteLoops {
|
296
|
352
|
int w = 0;
|
297
|
353
|
int numberOfVotes = voteTallies.length;
|
298
|
354
|
|
299
|
|
-
|
300
|
|
- // calling
|
301
|
|
- w = w + 1;
|
302
|
|
- // each time through the inner loop
|
|
355
|
+ for(int i=0; i<numberOfVotes; i++){
|
|
356
|
+ System.out.println(voteTallies[i]);
|
|
357
|
+ // calling
|
|
358
|
+ w = w + 1;
|
|
359
|
+ // each time through the inner loop
|
|
360
|
+ }
|
303
|
361
|
|
304
|
362
|
return w;
|
305
|
363
|
}
|
|
@@ -310,11 +368,15 @@ public class WriteLoops {
|
310
|
368
|
public int tallyVote2() {
|
311
|
369
|
int w = 0;
|
312
|
370
|
int numberOfVotes = voteTallies.length;
|
|
371
|
+ int idx = 0;
|
313
|
372
|
|
314
|
|
-
|
315
|
|
- // calling
|
316
|
|
- w = w + 1;
|
317
|
|
- // each time through the inner loop
|
|
373
|
+ while(idx<numberOfVotes){
|
|
374
|
+ System.out.println(voteTallies[idx]);
|
|
375
|
+ idx++;
|
|
376
|
+ // calling
|
|
377
|
+ w = w + 1;
|
|
378
|
+ // each time through the inner loop
|
|
379
|
+ }
|
318
|
380
|
|
319
|
381
|
return w;
|
320
|
382
|
}
|