FizzBuzz/Java
< FizzBuzz
FizzBuzz/Java is part of FizzBuzz. You may find other members of FizzBuzz at Category:FizzBuzz.
If/else ladder
public class FizzBuzz{
public static void main(String[] args){
for(int i= 1; i <= 100; i++){
if(i % 15 == 0){
System.out.println("FizzBuzz");
}else if(i % 3 == 0){
System.out.println("Fizz");
}else if(i % 5 == 0){
System.out.println("Buzz");
}else{
System.out.println(i);
}
}
}
}
Concatenation
public class FizzBuzz{
public static void main(String[] args){
for(int i= 1; i <= 100; i++){
String output = "";
if(i % 3 == 0) output += "Fizz";
if(i % 5 == 0) output += "Buzz";
if(output.equals("")) output += i;
System.out.println(output);
}
}
}
Ternary operator
public class FizzBuzz{
public static void main(String[] args){
for(int i= 1; i <= 100; i++){
System.out.println(i % 15 != 0 ? i % 5 != 0 ? i % 3 != 0 ?
i : "Fizz" : "Buzz" : "FizzBuzz");
}
}
}
Recursive
public String fizzBuzz(int n){
String s = "";
if (n == 0)
return s;
if((n % 5) == 0)
s = "Buzz" + s;
if((n % 3) == 0)
s = "Fizz" + s;
if (s.equals(""))
s = n + "";
return fizzBuzz(n-1) + "\n" + s;
}
Alternative Recursive
public String fizzBuzz(int n){
return (n>0) ? fizzBuzz(n-1) +
(n % 15 != 0? n % 5 != 0? n % 3 != 0? (n+"") :"Fizz" : "Buzz" : "FizzBuzz")
: "";
}
Using an array
class FizzBuzz {
public static void main( String [] args ) {
for( int i = 1 ; i <= 100 ; i++ ) {
System.out.println( new String[]{ i+"", "Fizz", "Buzz", "FizzBuzz" }[ ( i%3==0?1:0 ) + ( i%5==0?2:0 ) ]);
}
}
}
Lambda with Arrays
class FizzBuzz {
public static void main( String [] args ) {
int [] x = new int [100];
Arrays.setAll(x, j -> j);
Arrays.stream(x).forEach(i -> {
if(i == 0) return;
String output = "";
if(i % 3 == 0) output += "Fizz";
if(i % 5 == 0) output += "Buzz";
if(output.equals("")) output += i;
System.out.println(output);
});
}
}
Lambda with Streams
package fizzbuzz;
import static java.util.stream.IntStream.rangeClosed;
public interface FizzBuzz {
public static void main(String... arguments) {
rangeClosed(1, 100)
.mapToObj(i -> {
if (i % (3 * 5) == 0) {
return "FizzBuzz";
} else if (i % 3 == 0) {
return "Fizz";
} else if (i % 5 == 0) {
return "Buzz";
} else {
return Integer.toString(i);
}
})
.forEach(System.out::println)
;
}
}
OO style
public class FizzBuzz {
public static void main(String[] args) {
Sound sound = new Sound(3, "Fizz", new Sound(5, "Buzz"));
for (int i = 1; i <= 100; i++) {
System.out.println(sound.generate(i));
}
}
private static class Sound {
private final int trigger;
private final String onomatopoeia;
private final Sound next;
public Sound(int trigger, String onomatopoeia, Sound next) {
this.trigger = trigger;
this.onomatopoeia = onomatopoeia;
this.next = next;
}
public Sound(int trigger, String sound) {
this(trigger, sound, null);
}
public String generate(int i) {
StringBuilder sb = new StringBuilder();
generate(sb, i);
return sb.length() == 0 ? String.valueOf(i) : sb.toString();
}
private void generate(StringBuilder sb, int i) {
if (i % trigger == 0)
sb.append(onomatopoeia);
if (next != null)
next.generate(sb, i);
}
}
}
Enum-based Declarative
With Functional and OO elements
A new rule can be added by adding a single line.
import java.util.Arrays;
import java.util.function.IntPredicate;
import java.util.stream.IntStream;
public enum FizzBuzz {
Fizz(divisibleBy(3)),
Buzz(divisibleBy(5));
private final IntPredicate test;
FizzBuzz(IntPredicate test) {
this.test = test;
}
static IntPredicate divisibleBy(int i) {
return n -> n % i == 0;
}
String applyRule(int n) {
if (test.test(n))
return name();
return "";
}
static String print(int i) {
String byRules = Arrays.stream(values())
.map(rule -> rule.applyRule(i))
.collect(Collectors.joining());
if (byRules.isEmpty())
return i + "";
return byRules;
}
public static void main(String[] args) {
IntStream.rangeClosed(1, 100)
.mapToObj(FizzBuzz::print)
.forEach(System.out::println);
}
}