412. Fizz Buzz

Write a program that outputs the string representation of numbers from 1 to n.

But for multiples of three it should output “Fizz” instead of the number and for the multiples of five output “Buzz”. For numbers which are multiples of both three and five output “FizzBuzz”.

没啥好说的,一个替代关系。 仔细写case就好了

class Solution {
    public List fizzBuzz(int n) {
        List result = new ArrayList<>();
        for(int i = 1;i<=n;i++)
        {
            if(i%15==0)
            {
                result.add("FizzBuzz");
            }
            else if (i%5==0)
            {
                result.add("Buzz");
            }
            else if (i%3==0)
            {
               result.add ("Fizz");
            }
            else 
            {
                String s = Integer.toString(i);
                result.add(s);
            }
        }
        return result;
    }
}

你可能感兴趣的:(412. Fizz Buzz)