[Java] FizzBuzz problem

less than 1 minute read

I tried solving the FizzBuzz problem! I feel like it will be more refreshing, but I will try to solve it again.

FizzBuss problem

The integers from 1 to 100 are output to the screen in order.

However, “Fizz” for multiples of 3, “Buzz” for multiples of 5, and “FizzBuzz” for multiples of 3 and 5 are output instead of numbers.



public class FizzBuzz {
    public static void main(String[] args) {
        for (int i = 1; i <= 100; i++) {/ / for is a loop with a number from 1 to 100 ((1) initialization formula; (2) conditional formula; (4) iterative formula)
            //In the iterative expression, write the processing you want to do after iteration (i is always incremented by 1)
            if (i %3 == 0) {
                // multiples of 3 and multiples of 5
                if (i %5 == 0) {
                    System.out.println("FizzBuzz");
                    // when multiples of 3
                } else {
                    System.out.println("Fizz");
                }
            } else if (i %5 == 0) {// multiple of 5
                System.out.println("Buzz");
                // if none of the above apply
            } else {
                System.out.println(i);
            }
        }
    }
}

Tags:

Updated: