Quellcode durchsuchen

Fixed Inverted Fors and completed while loop methods

Trinh Tong vor 6 Jahren
Ursprung
Commit
c4477f8fd8
3 geänderte Dateien mit 41 neuen und 23 gelöschten Zeilen
  1. 37
    19
      WriteLoops.java
  2. 1
    1
      WriteLoopsTest.java
  3. 3
    3
      package.bluej

+ 37
- 19
WriteLoops.java Datei anzeigen

@@ -43,11 +43,11 @@ public class WriteLoops {
43 43
     }
44 44
 
45 45
     public int startAtTwentyOne() {
46
-        int w = 21;
46
+        int w = 0;
47 47
 
48 48
         // Write a FOR loop that makes 10 iterations, start at 21.
49
-        for (int i = 0; i < 10; i++) {
50
-            w = w - 1;
49
+        for (int i = 0; i <= 10; i++) {
50
+            w = w + 1;
51 51
             // each time through the loop
52 52
          }
53 53
         
@@ -73,7 +73,7 @@ public class WriteLoops {
73 73
         // Write a FOR loop from 0 to 32 by 2s.
74 74
         for (int i = 32; i > 2; i -= 2) {
75 75
             // calling
76
-            //w = w + 1;
76
+            w = w + 1;
77 77
             // each time through the loop
78 78
         }
79 79
         return w;
@@ -83,7 +83,7 @@ public class WriteLoops {
83 83
         int w = 0;
84 84
 
85 85
         // Write a FOR loop from 1 to less than 5001 by 11s.
86
-        for (int i = 5001; i > 1; i -= 11) {
86
+        for (int i = 1; i < 5001; i += 11) {
87 87
             // calling
88 88
             w = w + 1;
89 89
             // each time through the loop
@@ -97,7 +97,7 @@ public class WriteLoops {
97 97
 
98 98
         // Write a nested FOR loop(s), where one counts from
99 99
         // 0 to less than 20 and the inner one counts from 0 to 4
100
-        for (int i = 20; i > 0; i--) {
100
+        for (int i = 0; i < 20; i++) {
101 101
             for (int j = 0; j <= 4; j ++) {// calling
102 102
                 w = w + 1;
103 103
                 // each time through the inner loop
@@ -237,12 +237,13 @@ public class WriteLoops {
237 237
     // and if it is, add 7 to “i”
238 238
     public int loop50by7() {
239 239
         int w = 0;
240
-
241
-
242
-            // calling
243
-            w = w + 1;
244
-            // each time through the inner loop
240
+        int i = 7;
245 241
         
242
+        while (i < 50) {
243
+            i += 7;
244
+            w = w + 1;
245
+        }
246
+
246 247
         return w;
247 248
     }
248 249
 
@@ -275,11 +276,12 @@ public class WriteLoops {
275 276
         int w = 0;
276 277
         int sumOfThrees = 0;
277 278
 
278
- 
279
+        for (int index = 0; index < 7; index++) {
280
+            sumOfThrees += threes_array[index];
279 281
             // calling
280 282
             w = w + 1;
281 283
             // each time through the inner loop
282
-        
284
+        }
283 285
         System.out.print("The Sum is ");
284 286
         System.out.println(sumOfThrees);
285 287
 
@@ -291,10 +293,13 @@ public class WriteLoops {
291 293
     public int rewriteFooAsWhile() {
292 294
         int w = 0;
293 295
         int sumOfThrees = 0;
294
-
295
- 
296
+        int index = 0;
297
+        while (index <= 7) {
298
+            sumOfThrees += threes_array[index];
296 299
             // calling
297 300
             w = w + 1;
301
+            index ++;
302
+        }
298 303
             // each time through the inner loop
299 304
         
300 305
         System.out.print("The Sum is ");
@@ -313,7 +318,15 @@ public class WriteLoops {
313 318
     public int manageYardAndJunior() {
314 319
         int w = 0;
315 320
         boolean onTime = true;
321
+        boolean yardNeedsMowed = true;
316 322
 
323
+        while (isSummer() == true) {
324
+            if (yardNeedsMowed == true) {
325
+                yellAtJuniorToMowLawn();
326
+            }
327
+        }
328
+        
329
+        sendJuniorBackToSchool("Tuesday");
317 330
         // ADD YOUR CODE here.
318 331
  
319 332
             // be sure to call
@@ -331,11 +344,13 @@ public class WriteLoops {
331 344
     public int tallyVote1() {
332 345
         int w = 0;
333 346
         int numberOfVotes = voteTallies.length;
334
-
335
- 
347
+        for (int i = 0; i < numberOfVotes; i++) {
348
+            System.out.print(voteTallies[i]);
336 349
             // calling
337 350
             w = w + 1;
338 351
             // each time through the inner loop
352
+        }
353
+ 
339 354
         
340 355
         return w;
341 356
     }
@@ -346,12 +361,15 @@ public class WriteLoops {
346 361
     public int tallyVote2() {
347 362
         int w = 0;
348 363
         int numberOfVotes = voteTallies.length;
349
-
364
+        int index = 0;
365
+        while (index < numberOfVotes) {
366
+            System.out.print(voteTallies[index]);
367
+            index++;
350 368
 
351 369
             // calling
352 370
             w = w + 1;
353 371
             // each time through the inner loop
354
-        
372
+        }
355 373
         return w;
356 374
     }
357 375
 

+ 1
- 1
WriteLoopsTest.java Datei anzeigen

@@ -71,7 +71,7 @@ public class WriteLoopsTest
71 71
     public void Test2to32()
72 72
     {
73 73
         WriteLoops writeLoo1 = new WriteLoops();
74
-        assertEquals(0, writeLoo1.byTwoTo32());
74
+        assertEquals(15, writeLoo1.byTwoTo32());
75 75
     }
76 76
 
77 77
     @Test

+ 3
- 3
package.bluej Datei anzeigen

@@ -6,9 +6,9 @@ dependency2.from=WriteIFsTest
6 6
 dependency2.to=WriteIFs
7 7
 dependency2.type=UsesDependency
8 8
 editor.fx.0.height=722
9
-editor.fx.0.width=989
10
-editor.fx.0.x=371
11
-editor.fx.0.y=118
9
+editor.fx.0.width=800
10
+editor.fx.0.x=320
11
+editor.fx.0.y=75
12 12
 objectbench.height=111
13 13
 objectbench.width=1241
14 14
 package.divider.horizontal=0.6