//计算平均值
let meanValue = sum(number: 1,2,3)
print("计算出的平均值是\(meanValue)")
//函数嵌套
print("通过函数嵌套计算出的结果\(qiantao(number: 20))")
//闭包分离出偶数奇数
print("闭包分离出偶数奇数\(separate([1,2,3,54,65,34,65,23,65,236,5]))")
//计算平均值
func sum(number:Int ...) -> Int{
var sums = 0
for num in number {
sums = sums + num
}
let meanValue = sums / number.count
return meanValue
}
//函数嵌套
func qiantao(number:Int) -> Float{
let num = number
func add(a:Float) -> Float{
return a + 19.23
}
let all = add(a: Float(num))
return Float(all)
}
//闭包分离出偶数奇数
var separate = {
(numbers:[Int]) -> NSMutableArray in
var array = NSMutableArray()
for num in numbers {
var str:String?
if num % 2 == 0{
str = "\(num)是偶数"
}else{
str = "\(num)是奇数"
}
array.add(str!)
}
return array
}
网友评论