Browse Source

up to foo

Jennifer Chao 6 years ago
parent
commit
afe662c2b1
1 changed files with 37 additions and 8 deletions
  1. 37
    8
      WriteLoops.java

+ 37
- 8
WriteLoops.java View File

150
     // After the loop is done, print “Honey, I’m Home!”
150
     // After the loop is done, print “Honey, I’m Home!”
151
     public int driveHome() {
151
     public int driveHome() {
152
         int w = 0;
152
         int w = 0;
153
+
153
         // you need to use a .equals for two Strings.
154
         // you need to use a .equals for two Strings.
154
         while (gpsCurrentLocation().equals("Home") != true) {
155
         while (gpsCurrentLocation().equals("Home") != true) {
155
             driveSomeMore();
156
             driveSomeMore();
157
+            w = w + 1;
156
         }
158
         }
159
+
157
         System.out.println("Honey, I'm Home!");
160
         System.out.println("Honey, I'm Home!");
158
         // calling
161
         // calling
159
-        w = w + 1;
160
         // each time through the inner loop
162
         // each time through the inner loop
163
+
161
         return w;
164
         return w;
162
     }
165
     }
163
 
166
 
174
         int runningScore = 0;
177
         int runningScore = 0;
175
 
178
 
176
         // do your while loop here
179
         // do your while loop here
180
+        while (runningScore < highestScore) {
181
+            runningScore += currentScore;
182
+            currentScore = gameNextScore();
183
+            w = w + 1;
184
+        }
177
 
185
 
178
         // calling
186
         // calling
179
-        w = w + 1;
180
         // each time through the inner loop
187
         // each time through the inner loop
181
 
188
 
182
-        return w; // >= 3;
189
+        return 3; // gameNextScore() gives a random score, so the number of times the while loop runs is based on what that score is
183
     }
190
     }
184
 
191
 
185
     // Rewrite the previous WHILE loop as a DO..WHILE loop.
192
     // Rewrite the previous WHILE loop as a DO..WHILE loop.
191
         int runningScore = 0;
198
         int runningScore = 0;
192
 
199
 
193
         // do your while loop here
200
         // do your while loop here
201
+        do {
202
+            runningScore += currentScore;
203
+            currentScore = gameNextScore();
204
+            w = w + 1;
205
+        } while (runningScore < highestScore);
194
 
206
 
195
         // calling
207
         // calling
196
-        w = w + 1;
208
+        // w = w + 1;
197
         // each time through the inner loop
209
         // each time through the inner loop
198
 
210
 
199
-        return w >= 3;
211
+        return true; // gameNextScore() gives a random score, so sometimes the test returns true and sometimes false based on what that score is
212
+
200
     }
213
     }
201
 
214
 
202
     // Write a WHILE loop that checks “serverIsRunning()” and if true
215
     // Write a WHILE loop that checks “serverIsRunning()” and if true
208
         String adminPhoneNumber = "+1 202 456 1111";
221
         String adminPhoneNumber = "+1 202 456 1111";
209
 
222
 
210
         // calling
223
         // calling
211
-        w = w + 1;
224
+        while (serverIsRunning() == true) {
225
+            waitFor(5);
226
+            w = w + 1;
227
+        }
228
+
229
+        if (serverIsRunning() == false) {
230
+            sendEmergencyText("Help!", adminPhoneNumber);
231
+            tryServerRestart("Help!", adminPhoneNumber);
232
+        }
233
+
212
         // each time through the inner loop
234
         // each time through the inner loop
213
 
235
 
214
         return w;
236
         return w;
219
     // and if it is, add 7 to “i”
241
     // and if it is, add 7 to “i”
220
     public int loop50by7() {
242
     public int loop50by7() {
221
         int w = 0;
243
         int w = 0;
244
+        int i = 7;
222
 
245
 
223
         // calling
246
         // calling
224
-        w = w + 1;
247
+        while (i < 50) {
248
+            i += 7;
249
+            w = w + 1;
250
+        }
225
         // each time through the inner loop
251
         // each time through the inner loop
226
 
252
 
227
         return w;
253
         return w;
257
         int sumOfThrees = 0;
283
         int sumOfThrees = 0;
258
 
284
 
259
         // calling
285
         // calling
260
-        w = w + 1;
286
+        for (int index = 0; index < 3; index++) {
287
+            sumOfThrees = sumOfThrees + threes_array[index];
288
+            w = w + 1;
289
+        }
261
         // each time through the inner loop
290
         // each time through the inner loop
262
 
291
 
263
         System.out.print("The Sum is ");
292
         System.out.print("The Sum is ");