mirror of
https://github.com/sigmasternchen/javagony2
synced 2025-03-15 03:38:54 +00:00
added fizzbuzz without custom number type
This commit is contained in:
parent
19a452db24
commit
fa6be261c9
1 changed files with 46 additions and 0 deletions
46
fizzbuzz/Fizzbuzz.java
Normal file
46
fizzbuzz/Fizzbuzz.java
Normal file
|
@ -0,0 +1,46 @@
|
|||
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();
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue