ソースを参照

Cleaned up code

mbowe4 6 年 前
コミット
ecb1913733

+ 10
- 29
src/main/java/io/zipcoder/microlabs/mastering_loops/TriangleUtilities.java ファイルの表示

@@ -4,46 +4,27 @@ public class TriangleUtilities {
4 4
 
5 5
     public static String getTriangle(int numberOfRows) {
6 6
         StringBuilder builder = new StringBuilder();
7
-        for (int row = 1; row < numberOfRows; row++) {
8
-            for (int col = 0; col < row; col++) {
9
-                builder.append("*");
10
-            }
11
-            builder.append("\n");
7
+        for (int row = 0; row < numberOfRows; row++) {
8
+            builder.append(getRow(row));
12 9
         }
13
-
14 10
         return builder.toString();
15
-
16 11
     }
17 12
 
18 13
     public static String getRow(int numberOfStars) {
19
-        String asterisk = "";
20
-        for (int i = 0; i < numberOfStars; i++) {
21
-            asterisk += "*";
14
+        StringBuilder builder = new StringBuilder();
15
+
16
+        for (int i = 0; i <= numberOfStars; i++) {
17
+            builder.append("*");
22 18
         }
23
-        return asterisk;
19
+        builder.append("\n");
20
+        return builder.toString();
24 21
     }
25 22
 
26 23
     public static String getSmallTriangle() {
27
-        StringBuilder builder = new StringBuilder();
28
-        for (int row = 1; row <= 4; row++) {
29
-            for (int col = 0; col < row; col++) {
30
-                builder.append("*");
31
-            }
32
-            builder.append("\n");
33
-        }
34
-
35
-        return builder.toString();
24
+        return getTriangle(4);
36 25
     }
37 26
 
38 27
     public static String getLargeTriangle() {
39
-        StringBuilder builder = new StringBuilder();
40
-        for (int row = 1; row <= 9; row++) {
41
-            for (int col = 0; col < row; col++) {
42
-                builder.append("*");
43
-            }
44
-            builder.append("\n");
45
-        }
46
-
47
-        return builder.toString();
28
+        return getTriangle(9);
48 29
     }
49 30
 }

+ 2
- 2
src/test/java/io/zipcoder/microlabs/mastering_loops/TriangleUtilitiesTest.java ファイルの表示

@@ -18,7 +18,7 @@ public class TriangleUtilitiesTest {
18 18
     @Test
19 19
     public void getTriangleTest1() {
20 20
         String expected =
21
-                "*\n" +
21
+                        "*\n" +
22 22
                         "**\n" +
23 23
                         "***\n" +
24 24
                         "****\n" +
@@ -27,7 +27,7 @@ public class TriangleUtilitiesTest {
27 27
                         "*******\n" +
28 28
                         "********\n" +
29 29
                         "*********\n";
30
-        String actual = TriangleUtilities.getTriangle(10);
30
+        String actual = TriangleUtilities.getTriangle(9);
31 31
         Assert.assertEquals(expected, actual);
32 32
     }
33 33