Advertisement
scottashipp

Extensible FizzBuzz

May 24th, 2013
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.47 KB | None | 0 0
  1. /*
  2.  * FizzBuzzBazz
  3.  * Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of
  4.  * the number and for the multiples of five print “Buzz” and for multiples of six print "Bazz."
  5.  * For numbers which are multiples of both three, five, and seven print “FizzBuzzBazz”.
  6.  *
  7.  * Make it so that it will be easy to add additional values like "foo" and "bar" in the future.
  8.  *
  9.  * Original FizzBuzz:
  10.  * Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of
  11.  * the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and
  12.  * five print “FizzBuzz”.
  13.  *
  14.  *
  15.  */
  16.  
  17. public class FizzBuzzBazz {
  18.    
  19.     public enum FizzBuzz {
  20.         FIZZ, BUZZ, BAZZ;
  21.        
  22.         public String toString() {
  23.             return name().charAt(0) + name().substring(1,name().length()).toLowerCase();
  24.         }
  25.        
  26.         public int toInt() {
  27.             switch(this) {
  28.             case FIZZ:
  29.                 return 3;
  30.             case BUZZ:
  31.                 return 5;
  32.             case BAZZ:
  33.             default:
  34.                 return 6;
  35.             }          
  36.         }
  37.     }
  38.    
  39.  
  40.     public static void main(String[] args) {
  41.        
  42.         String toOutput;
  43.         for(int i=1; i <=100; i++) {
  44.             toOutput = Integer.toString(i);
  45.             for(FizzBuzz f : FizzBuzz.values()) {
  46.                
  47.                 if(i % f.toInt() ==0) {
  48.                     if(toOutput.equals(Integer.toString(i)))
  49.                         toOutput = f.toString();
  50.                     else
  51.                         toOutput += f.toString();
  52.                 }
  53.                
  54.             }
  55.             System.out.println(toOutput);
  56.         }
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement