我怎么能在循环中更改变量的名称? 比如number1,number2,number3,number4?
var array = [2,4,6,8]
func ap ( number1: Int , number2: Int , number3: Int , number4: Int ) {
for (index,_) in array.enumerated() {
array[index] += **("number\(index)")**
}
print(array)
}
ap(number1: 2, number2: 4, number3: 6, number4: 8)
答案 0 :(得分:0)
我认为这个answer说不可能,而是使用数组。
这是使用另一个数组:
var array = [2,4,6,8]
func ap ( number1: Int , number2: Int , number3: Int , number4: Int ) {
let arrayOfParameters = [number1, number2, number3, number4]
for (index,_) in array.enumerated() {
array[index] += arrayOfParameters[index]
}
print(array)
}
ap(number1: 2, number2: 4, number3: 6, number4: 8)
打印:[4,8,12,16]