FizzBuzz/Java: Difference between revisions
Content added Content deleted
No edit summary |
No edit summary |
||
Line 111: | Line 111: | ||
public static void main(String[] args) { |
public static void main(String[] args) { |
||
Sound sound = new Sound(3, "Fizz", new Sound(5, "Buzz")); |
|||
for (int i = 1; i <= 100; i++) { |
for (int i = 1; i <= 100; i++) { |
||
System.out.println( |
System.out.println(sound.generate(i)); |
||
} |
} |
||
} |
} |
||
private static class |
private static class Sound { |
||
private final int trigger; |
private final int trigger; |
||
private final String |
private final String onomatopoeia; |
||
⚫ | |||
public |
public Sound(int trigger, String onomatopoeia, Sound next) { |
||
this.trigger = trigger; |
this.trigger = trigger; |
||
this. |
this.onomatopoeia = onomatopoeia; |
||
⚫ | |||
} |
} |
||
public |
public Sound(int trigger, String sound) { |
||
this(trigger, sound, null); |
|||
} |
} |
||
⚫ | |||
⚫ | |||
private static class SoundMakerGroup { |
|||
StringBuilder sb = new StringBuilder(); |
|||
⚫ | |||
⚫ | |||
⚫ | |||
public SoundMakerGroup(SoundMaker... soundMakers) { |
|||
⚫ | |||
this.soundMakers = soundMakers; |
|||
⚫ | |||
private void generate(StringBuilder sb, int i) { |
|||
if (i % trigger == 0) |
|||
sb.append(onomatopoeia); |
|||
if (next != null) |
|||
next.generate(sb, i); |
|||
} |
} |
||
⚫ | |||
⚫ | |||
for (SoundMaker soundMakers : soundMakers) { |
|||
⚫ | |||
} |
|||
⚫ | |||
⚫ | |||
} |
} |
||