二元运算符" =="不能应用于类型的操作数(Int,Int,Int,Int) - >诠释

时间:2017-04-06 21:11:45

标签: swift int binary-operators

var a11: Int = 0
var a12: Int = 0
var a21: Int = 0
var a22: Int = 0

var valueDeterminant = a11 * a12 * a21 + a22

func calculateDeterminant(a11: Int, a12: Int, a21: Int, a22: Int) -> Int {
    return valueDeterminant
}


if calculateDeterminant == 0 {
    print("The trasformation is irreversable, due to the fact that the determinant is \(calculateDeterminant)")
 } else if calculateDeterminant == 1 {
    print("The determinant is\(calculateDeterminant) \nThe trasformation is reverable, and it keeps the area")
 } else {
    print("The determinant is \(calculateDeterminant) \nThe transformation is reversable")
}

错误表示二元运算符==不能用于(Int,Int,Int,Int)类型。为什么?我该如何解决这个问题? 此外,如果我将返回值更改为Bool,则会出现另一个错误,表示我无法输入返回Bool值的Int值。

1 个答案:

答案 0 :(得分:0)

函数是第一类值。您正在将函数calculateDeterminantInt进行比较。不支持。调用函数,然后比较整数值。

var a11: Int = 0
var a12: Int = 0
var a21: Int = 0
var a22: Int = 0

var valueDeterminant = a11 * a12 * a21 + a22

func calculateDeterminant() -> Int {
  return a11 * a12 * a21 * a22
}


if calculateDeterminant() == 0 {
  print("The trasformation is irreversable, due to the fact that the determinant is \(calculateDeterminant())")
} else if calculateDeterminant() == 1 {
  print("The determinant is\(calculateDeterminant()) \nThe trasformation is reverable, and it keeps the area")
} else {
  print("The determinant is \(calculateDeterminant()) \nThe transformation is reversable")
}

或者像这样定义......

func calculateDeterminant(a11: Int, a12: Int, a21: Int, a22: Int) -> Int {
    return a11 * a12 * a21 * a22
}

......并且这样称呼它。

if (calculateDeterminant(a11: a11, a12: a12, a21: a21, a22: a22) == 0 {...}