Kthomas 6 lat temu
rodzic
commit
d3ee7806da

+ 26
- 53
src/main/java/io/zipcoder/microlabs/mastering_loops/NumberUtilities.java Wyświetl plik

@@ -1,93 +1,66 @@
1 1
 package io.zipcoder.microlabs.mastering_loops;
2 2
 
3
-
4 3
 public class NumberUtilities {
5 4
     public static String getEvenNumbers(int start, int stop) {
6
-        String numList = "";
7
-
5
+        StringBuilder numList = new StringBuilder();
8 6
         for (int i = start; i < stop; i++) {
9
-
10 7
             if (i % 2 == 0) {
11
-
12
-                numList = numList + i;
8
+                numList.append(i);
13 9
             }
14 10
         }
15 11
 
16
-
17
-        return numList;
12
+        return numList.toString();
18 13
     }
19 14
 
20
-
21 15
     public static String getOddNumbers(int start, int stop) {
22
-
23
-        String numList = "";
24
-
16
+        StringBuilder numList = new StringBuilder();
25 17
         for (int i = start; i < stop; i++) {
26
-
27 18
             if (i % 2 != 0) {
28
-
29
-                numList = numList + i;
19
+                numList.append(i);
30 20
             }
31 21
         }
32
-        return numList;
33
-    }
34 22
 
23
+        return numList.toString();
24
+    }
35 25
 
36 26
     public static String getSquareNumbers(int start, int stop, int step) {
37
-
38
-        String numList = "";
39
-
27
+        StringBuilder numList = new StringBuilder();
40 28
         for (int i = start; i < stop; i += step) {
41
-
42
-            int theLid = (int) Math.pow(i, 2);
43
-            numList += String.valueOf(theLid);
44
-
29
+            int x = (int) Math.pow(i, 2);
30
+            numList.append(String.valueOf(x));
45 31
         }
46 32
 
47
-        return numList;
33
+        return numList.toString();
48 34
     }
49 35
 
50
-
51 36
     public static String getRange(int stop) {
52
-        String numList = "";
53
-
37
+        StringBuilder numList = new StringBuilder();
54 38
         for (int i = 0; i < stop; i++)
55
-            numList += String.valueOf(i);
56
-
57
-        return numList;
39
+            numList.append(String.valueOf(i));
40
+        return numList.toString();
58 41
     }
59 42
 
60
-
61
-
62 43
     public static String getRange(int start, int stop) {
63
-        String numList = "";
64
-
44
+        StringBuilder numList = new StringBuilder();
65 45
         for (int i = start; i < stop; i++)
66
-            numList += String.valueOf(i);
67
-
68
-        return numList;
46
+            numList.append(String.valueOf(i));
47
+        return numList.toString();
69 48
     }
70 49
 
71
-
72 50
     public static String getRange(int start, int stop, int step) {
73
-        String numList = "";
74
-
51
+        StringBuilder numList = new StringBuilder();
75 52
         for (int i = start; i < stop; i+=step)
76
-            numList += String.valueOf(i);
77
-
78
-        return numList;
53
+            numList.append(String.valueOf(i));
54
+        return numList.toString();
79 55
     }
80 56
 
81
-
82 57
     public static String getExponentiations(int start, int stop, int step, int exponent) {
83
-
84
-        String numList = "";
85
-
58
+        StringBuilder numList = new StringBuilder();
86 59
         for (int i = start; i < stop; i += step) {
87
-
88
-            int theLid = (int) Math.pow(i, exponent);
89
-            numList += String.valueOf(theLid);
60
+            int x = (int) Math.pow(i, exponent);
61
+            numList.append(String.valueOf(x));
90 62
         }
91
-        return numList;
63
+
64
+        return numList.toString();
92 65
     }
93
-}
66
+}