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”.
Example:
n = 15,
Return:
[
"1",
"2",
"Fizz",
"4",
"Buzz",
"Fizz",
"7",
"8",
"Fizz",
"Buzz",
"11",
"Fizz",
"13",
"14",
"FizzBuzz"
]
这题是比较简单的,如果说想考察什么,那就是靠谱分支判断的顺序吧。这里先判断FizzBuzz的情况,即能同时整除3和5,如果不是,则看是否能整除3以及整除5,最后都不是就是一般整型转字符串。
当然了,本题有点需要注意的地方
i % 3 == 0 && i % 5 == 0
可以转换为i % 15 == 0
- Go的switch是从上往下一个case一个case的查找,找到对应的执行完退出switch的block