Swift完成fizz buzz test

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

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

你可能感兴趣的:(test,swift,buzz,fizz)