Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * FizzBuzzBazz
- * Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of
- * the number and for the multiples of five print “Buzz” and for multiples of six print "Bazz."
- * For numbers which are multiples of both three, five, and seven print “FizzBuzzBazz”.
- *
- * Make it so that it will be easy to add additional values like "foo" and "bar" in the future.
- *
- * Original FizzBuzz:
- * Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of
- * the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and
- * five print “FizzBuzz”.
- *
- *
- */
- public class FizzBuzzBazz {
- public enum FizzBuzz {
- FIZZ, BUZZ, BAZZ;
- public String toString() {
- return name().charAt(0) + name().substring(1,name().length()).toLowerCase();
- }
- public int toInt() {
- switch(this) {
- case FIZZ:
- return 3;
- case BUZZ:
- return 5;
- case BAZZ:
- default:
- return 6;
- }
- }
- }
- public static void main(String[] args) {
- String toOutput;
- for(int i=1; i <=100; i++) {
- toOutput = Integer.toString(i);
- for(FizzBuzz f : FizzBuzz.values()) {
- if(i % f.toInt() ==0) {
- if(toOutput.equals(Integer.toString(i)))
- toOutput = f.toString();
- else
- toOutput += f.toString();
- }
- }
- System.out.println(toOutput);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement