Swift完成fizz buzz test
发布时间:2020-12-14 06:48:04 所属栏目:百科 来源:网络整理
导读:看到一篇文章上说,很多貌似看过很多本编程书的童鞋连简单的fizz buzz测试都完不成. 不知道fizz buzz test为何物的,建议自行搜之. 测试要求是,编写满足以下条件的代码: Write a program that prints the numbers from 1 to 100. But for multiples of three p
看到一篇文章上说,很多貌似看过很多本编程书的童鞋连简单的fizz buzz测试都完不成. 不知道fizz buzz test为何物的,建议自行搜之. 测试要求是,编写满足以下条件的代码: Write a program that prints the numbers from 1 to 100. But
for multiples of three print “Fizz” instead of the number
which are multiples of both three and five print
“FizzBuzz”.
更通俗的说就是: For each integer between 1 and 100,inclusive:
If the number is divisible by '3',then print "Fizz"
If the number is divisible by '5',then print "Buzz"
If the number is divisible by both '3' and '5',then print "FizzBuzz"
Otherwise,print the number.
本猫用Swift的解决方案如下: for x in 1...100{
if x % 3 == 0 && x % 5 == 0{
print("FizzBuzz")
}else if x % 3 == 0{
print("Fizz")
}else if x % 5 == 0{
print("Buzz")
}else{
print(x)
}
}
好吧,我承认是超级简单…我是有够无聊… ;[ (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |