Java is used as the development language for business. Outside of work, I create portfolios in Ruby.
FizzBuzz is a program that displays from one number to another on the screen. However, there are the following conditions. ・ When it is a multiple of 3, it is displayed as Fizz instead of a number. ・ When it is a multiple of 5, Buzz is displayed instead of the number. ・ When it is a multiple of 15, FizzBuzz is displayed instead of the number.
Java
class FizzBuzz {
public static void main(String args[]) {
for (int i = 1; i <= 15; i++) {
if (i % 15 == 0) {
System.out.printIn("FizzBuzz");
} else if (i % 3 == 0) {
System.out.printIn("Fizz");
} else if (i % 5 == 0) {
System.out.printIn("Buzz");
} else {
System.out.printIn(i);
}
}
}
}
Ruby
(1..15).each do |i|
if i % 15 == 0
puts 'FizzBuzz'
elsif i % 3 == 0
puts 'Fizz'
elsif i % 5 == 0
puts 'Buzz'
else
puts i
end
end
Recommended Posts