import java.util.*;
import java.lang.*;
import java.io.*;
/**
* Create a list of strings for the numbers 1 through n (inclusive) with the following rules:
* If the number is divisible by 3, use "Fizz"
* If the number is divisible by 5, use "Buzz"
* If the number is divisible by 3 and 5, use "FizzBuzz"
* Otherwise, use the number
*
* For n = 5, the output should be:
* [1, 2, Fizz, 4, Buzz]
*
* For n = 15, the output should be:
* [1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14, FizzBuzz]
*/
class Main {
public static void main(String[] args) {
int n = 15;
final List<String> output = fizzBuzz(n);
System.out.println(output);
}
public static List<String> fizzBuzz(final int n) {
final var output = new ArrayList<String>();
for (int i = 1; i <= n; ++i) {
// Tricky! One of the main points of this question is to make sure the developer
// remembers to put this condition check first.
if (i % 3 == 0 && i % 5 == 0) {
output.add("FizzBuzz");
} else if (i % 3 == 0) {
output.add("Fizz");
} else if (i % 5 == 0) {
output.add("Buzz");
} else {
output.add(String.valueOf(i));
}
}
return output;
}
}
To embed this program on your website, copy the following code and paste it into your website's HTML: