mirror of
https://github.com/sigmasternchen/javagony2
synced 2025-03-15 11:48:54 +00:00
47 lines
974 B
Java
47 lines
974 B
Java
![]() |
public class Fizzbuzz {
|
||
|
|
||
|
@FunctionalInterface
|
||
|
static interface Action {
|
||
|
void execute();
|
||
|
}
|
||
|
|
||
|
@FunctionalInterface
|
||
|
static interface Consumer {
|
||
|
void take(int i);
|
||
|
}
|
||
|
|
||
|
static Action nop = () -> {};
|
||
|
|
||
|
static void executeIfElse(int condition, Action ifAction, Action elseAction) {
|
||
|
Action[] branches = new Action[condition + 1];
|
||
|
branches[condition] = ifAction;
|
||
|
branches[0] = elseAction;
|
||
|
branches[condition].execute();
|
||
|
}
|
||
|
|
||
|
static void forLoop(int start, int end, Consumer each) {
|
||
|
executeIfElse(end - start, () -> {
|
||
|
each.take(start);
|
||
|
forLoop(start + 1, end, each);
|
||
|
}, nop);
|
||
|
}
|
||
|
|
||
|
public static void main(String[] args) {
|
||
|
forLoop(1, 20, i -> {
|
||
|
executeIfElse(i % 3, () -> {
|
||
|
executeIfElse(i % 5, () -> {
|
||
|
System.out.print(i);
|
||
|
}, () -> {
|
||
|
System.out.print("Buzz");
|
||
|
});
|
||
|
}, () -> {
|
||
|
System.out.print("Fizz");
|
||
|
executeIfElse(i % 5, nop, () -> {
|
||
|
System.out.print(" Buzz");
|
||
|
});
|
||
|
});
|
||
|
System.out.println();
|
||
|
});
|
||
|
}
|
||
|
}
|