您当前的位置: 首页 >  swift

Swift完成fizz buzz test

发布时间:2016-08-17 16:59:48 ,浏览量:0

看到一篇文章上说,很多貌似看过很多本编程书的童鞋连简单的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)
	}
}

好吧,我承认是超级简单…我是有够无聊… ;[

关注
打赏
1688896170
查看更多评论

暂无认证

  • 0浏览

    0关注

    107766博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录

0.0754s