fork download
  1. import java.util.AbstractMap.SimpleEntry;
  2. import java.util.Map.Entry;
  3.  
  4. class FizzBuzzQuiz {
  5. private static ThreadLocal local = new ThreadLocal();
  6.  
  7. private static Object fizzBuzz(Object x, Integer n, String msg) {
  8. if (x instanceof Integer) {
  9. local.set(new SimpleEntry((Integer)x, ""));
  10. }
  11. Entry<Integer,String> state = (Entry)local.get();
  12. if (state.getKey() % n == 0) {
  13. state.setValue(state.getValue() + msg);
  14. }
  15. if (state.getValue() == "") {
  16. return state.getKey();
  17. } else {
  18. return state.getValue();
  19. }
  20. }
  21.  
  22. public static Object fizz(Object x) { return fizzBuzz(x, 3, "Fizz"); }
  23. public static Object buzz(Object x) { return fizzBuzz(x, 5, "Buzz"); }
  24. public static Object gizz(Object x) { return fizzBuzz(x, 7, "Gizz"); }
  25.  
  26. public static void main (String[] args) {
  27. System.out.println( gizz(buzz(fizz(1))) );
  28. System.out.println( gizz(buzz(fizz(3))) );
  29. System.out.println( gizz(buzz(fizz(5))) );
  30. System.out.println( gizz(buzz(fizz(15))) );
  31. System.out.println( gizz(buzz(fizz(7))) );
  32. System.out.println( gizz(buzz(fizz(21))) );
  33. System.out.println( gizz(buzz(fizz(35))) );
  34. System.out.println( gizz(buzz(fizz(105))) );
  35. System.out.println( buzz(gizz(fizz(105))) );
  36. System.out.println( gizz(buzz(fizz(999))) );
  37.  
  38. System.out.println( (int)buzz(fizz(1)) + 2 );
  39. System.out.println( fizz((int)buzz(fizz(1)) + 2) );
  40. System.out.println( (String)buzz(fizz(15)) + "XXX" );
  41.  
  42. }
  43. }
Success #stdin #stdout 0.07s 380224KB
stdin
Standard input is empty
stdout
1
Fizz
Buzz
FizzBuzz
Gizz
FizzGizz
BuzzGizz
FizzBuzzGizz
FizzGizzBuzz
Fizz
3
Fizz
FizzBuzzXXX