-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFizzBuzzArray.java
More file actions
41 lines (36 loc) · 1.16 KB
/
FizzBuzzArray.java
File metadata and controls
41 lines (36 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Print a list of numbers from 1 to 100.
// Print FizzBuzz for numbers divisible by 3 and 5.
// Print Fizz for numbers divisible by 3, and Buzz for numbers divisible by 5.
// Print the other numbers as they would normally appear.
import java.io.Console;
import java.util.Arrays;
public class FizzBuzzArray {
public static void main(String []args) {
String[] fizzBuzzArray = new String[100];
fizzBuzzArray = fizzBuzzArrayBuilder(fizzBuzzArray);
fizzBuzzArrayPrinter(fizzBuzzArray);
}
public static String[] fizzBuzzArrayBuilder(String[] fizzBuzzArray) {
int range = 100;
for (int i = 1; i <= range; i++) {
int n = i-1;
if (i % 15 == 0) {
fizzBuzzArray[n] = "FizzBuzz!";
} else if ( i % 3 == 0) {
fizzBuzzArray[n] = "Fizz";
} else if ( i % 5 == 0) {
fizzBuzzArray[n] = "Buzz";
} else {
fizzBuzzArray[n] = Integer.toString(i);
}
}
return fizzBuzzArray;
}
public static void fizzBuzzArrayPrinter(String[] fizzBuzzArray) {
Console cons = System.console();
int l = fizzBuzzArray.length;
for (int i = 0; i < l; i++) {
cons.printf("\n%s", fizzBuzzArray[i]);
}
}
}