二元运算符“/"不能应用于两个 (Int) 操作数 [英] Binary operator '/' cannot be applied to two (Int) operands

查看:48
本文介绍了二元运算符“/"不能应用于两个 (Int) 操作数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我将以下代码放入 Xcode 的 Swift 操场中时,出现 二元运算符/"无法应用于两个 (Int) 操作数 错误.

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.

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

上面是一个计算任意数字总和的函数.下面是一个计算数字平均值的函数.该函数正在从自身内部调用 sumOf() 函数.

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.

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);

注意:我在堆栈交换中到处寻找答案,但问题都与我的不同,因为我的问题涉及两个 Int,相同的类型而不是不同的两个不同的类型.

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.

如果有人能帮我解决我遇到的问题,我会很高兴.

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

推荐答案

尽管有错误消息,但您似乎无法转发序列 (...) 运算符.在 agv() 函数中对 sumOf(numbers) 的单次调用会给出错误 cannot invoke sumOf with a type ((Int))

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))

这篇关于二元运算符“/"不能应用于两个 (Int) 操作数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆