看到一篇文章上说,很多貌似看过很多本编程书的童鞋连简单的fizz buzz测试都完不成.
不知道fizz buzz test为何物的,建议自行搜之.
测试要求是,编写满足以下条件的代码:
Write a program that prints the numbers @H_502_12@from 1 @H_502_12@to 100. But
for multiples of three print “Fizz” instead of the @H_502_12@number
which are multiples of both three and five print
“FizzBuzz”.
更通俗的说就是:
For each integer between 1 and 100,inclusive:
If the @H_502_12@number is divisible by '3',then print "Fizz"
If the @H_502_12@number is divisible by '5',then print "Buzz"
If the @H_502_12@number is divisible by both '3' and '5',then print "FizzBuzz"
Otherwise,print the @H_502_12@number.
本猫用Swift的解决方案如下:
for x in 1...100{
if x % 3 == 0 && x % 5 == 0{
@H_502_12@print("FizzBuzz")
}else if x % 3 == 0{
@H_502_12@print("Fizz")
}else if x % 5 == 0{
@H_502_12@print("Buzz")
}else{
@H_502_12@print(x)
}
}
好吧,我承认是超级简单…我是有够无聊… ;[