如何解决此'<='不是前缀一元运算符错误?

时间:2019-06-04 18:59:48

标签: swift

“ if-else”部分出现错误。

我正在操场上做Swift。

let namescore = arc4random_uniform(101)

if  namescore > 51{
    return "high match of \(namescore)"
} else if  (namescore, >=40 && namescore, <=40) {
    return "avg match of \(namescore)"
}
  

错误:“ <=”不是前缀一元运算符
  错误:'> ='不是前缀一元运算符

1 个答案:

答案 0 :(得分:2)

“ 40”旁边的“ =”令人困惑,而Swift(我认为您使用的是“ Fix”,其中添加了逗号)。这是您的代码的有效版本:

import Foundation

func getScore() -> String {
    let namescore = arc4random_uniform(101)

    if namescore > 51 {
        return "high match of \(namescore)"
    } else if (namescore >= 40 && namescore <= 40) {
        return "avg match of \(namescore)"
    } else {
        return "whatever"
    }
}

getScore()

您对40的比较可以简化为直接相等比较。

import Foundation

func getScore() -> String {
    let namescore = arc4random_uniform(101)

    if namescore > 51 {
        return "high match of \(namescore)"
    } else if namescore == 40 {
        return "avg match of \(namescore)"
    } else {
        return "whatever"
    }
}

getScore()