From fa6be261c98362342d4c9e1b81bd37f4ceb7f55d Mon Sep 17 00:00:00 2001 From: overflowerror Date: Sun, 6 Jun 2021 00:01:20 +0200 Subject: [PATCH] added fizzbuzz without custom number type --- fizzbuzz/Fizzbuzz.java | 46 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 fizzbuzz/Fizzbuzz.java diff --git a/fizzbuzz/Fizzbuzz.java b/fizzbuzz/Fizzbuzz.java new file mode 100644 index 0000000..19853f7 --- /dev/null +++ b/fizzbuzz/Fizzbuzz.java @@ -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(); + }); + } +}