I tried to solve the FizzBuzz problem! I feel like it's more refreshing, but I'll try to solve it again.
Integers from 1 to 100 are output to the screen in order.
However, "Fizz" is output for multiples of 3, "Buzz" for multiples of 5, and "FizzBuzz" for multiples of 3 and 5 on the screen instead of numbers.
public class FizzBuzz {
public static void main(String[] args) {
for (int i = 1; i <= 100; i++) { //Loop with a number from 1 to 100 with for(① Initialization formula;② Conditional expression;④ Iterative)
//In iterative formula, write the process you want to do after iterative(When repeating, i is always+1 to be)
if (i % 3 == 0) {
//When it is a multiple of 3 and a multiple of 5
if (i % 5 == 0) {
System.out.println("FizzBuzz");
//When it is a multiple of 3
} else {
System.out.println("Fizz");
}
} else if (i % 5 == 0) { //When it is a multiple of 5
System.out.println("Buzz");
//If none of the above apply
} else {
System.out.println(i);
}
}
}
}
Recommended Posts