二进制运算符'/'不能应用于两个(Int)操作数[duplicate]

时间:2023-01-23 22:10:11

This question already has an answer here:

这个问题在这里已有答案:

I am getting a Binary operator '/' cannot be applied to two (Int) operands error when I put the following code in a Swift playground in Xcode.

当我将以下代码放在Xcode的Swift操场中时,我得到一个二进制运算符'/'不能应用于两个(Int)操作数错误。

func sumOf(numbers: Int...) -> Int {
    var sum = 0
    for number in numbers {
        sum += number
    }
    return sum
}
sumOf()
sumOf(42, 597, 12)

The above was a function calculating the total sum of any numbers. Below is a function calculating the average of the numbers. The function is calling the sumOf() function from within itself.

以上是计算任何数字总和的函数。下面是计算数字平均值的函数。该函数从内部调用sumOf()函数。

func avg(numbers: Int...) -> Float {
    var avg:Float = ( sumOf(numbers) ) / ( numbers.count ) //Binary operator '/' cannot be applied to two (Int) operands
    return avg
}

avg(1, 2, 3);

Note: I have looked everywhere in stack exchange for the answer, but the questions all are different from mine because mine is involving two Ints, the same type and not different two different types.

注意:我已经在堆栈交换中随处查找答案,但问题都与我的不同,因为我的涉及两个Ints,相同类型而不是两个不同类型。

I would like it if someone could help me to solve the problem which I have.

如果有人能帮助我解决我所遇到的问题,我希望如此。

2 个解决方案

#1


1  

Despite the error message it seems that you cannot forward the sequence (...) operator. A single call of sumOf(numbers) within the agv() function gives an error cannot invoke sumOf with an argument of type ((Int))

尽管出现错误消息,但似乎无法转发序列(...)运算符。在agv()函数中单次调用sumOf(数字)会导致错误无法使用类型((Int))的参数调用sumOf

#2


-3  

The error is telling you what to do. If you refer to https://developer.apple.com/library/mac/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_operators.html

错误告诉你该怎么做。如果您参考https://developer.apple.com/library/mac/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_operators.html

/  Division.
A binary arithmetic operator that divides the number to its left by the number to its right.
Class of operands: integer, real
Class of result: real    

The second argument has to be real. Convert it like so. I don't use xcode, but I think my syntax is correct.

第二个论点必须是真实的。像这样转换它。我不使用xcode,但我认为我的语法是正确的。

var avg:Float = ( sumOf(numbers) ) / Float( numbers.count ) 

#1


1  

Despite the error message it seems that you cannot forward the sequence (...) operator. A single call of sumOf(numbers) within the agv() function gives an error cannot invoke sumOf with an argument of type ((Int))

尽管出现错误消息,但似乎无法转发序列(...)运算符。在agv()函数中单次调用sumOf(数字)会导致错误无法使用类型((Int))的参数调用sumOf

#2


-3  

The error is telling you what to do. If you refer to https://developer.apple.com/library/mac/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_operators.html

错误告诉你该怎么做。如果您参考https://developer.apple.com/library/mac/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_operators.html

/  Division.
A binary arithmetic operator that divides the number to its left by the number to its right.
Class of operands: integer, real
Class of result: real    

The second argument has to be real. Convert it like so. I don't use xcode, but I think my syntax is correct.

第二个论点必须是真实的。像这样转换它。我不使用xcode,但我认为我的语法是正确的。

var avg:Float = ( sumOf(numbers) ) / Float( numbers.count )