编写一个函数,该函数接收两个Int类型的数组作为输入,如果第一个数组中的数字之和大于第二个数组中的数字之和,则返回true;否则返回false
老实说,我已阅读文档,将其搜索出来,但没有找到答案。我编写的代码没有显示False或True
func someFunction(numbers: Int, numbers2: Int) {
let numbers = [1, 2, 3, 4, 5]
let addTwo: (Int, Int) -> Int = { x, y in x + y }
let sumOne = numbers.reduce(0, addTwo)
let numbers2 = [6, 2, 3, 4, 5]
let addTwo2: (Int, Int) -> Int = { x, y in x + y }
let sumTwo = numbers2.reduce(0, addTwo2)
if sumOne < sumTwo {
print("True")
} else {
sumTwo > sumTwo
print("False")
}
}
XСODE会写“ Done”(全部)。
您需要的是:
func compare(_ first: [Int], and second: [Int]) -> Bool {
return first.reduce(0, +) > second.reduce(0, +)
}
此用法:
print(compare([1, 2, 3, 4, 5], and: [6, 2, 3, 4, 5]))