java - 三个运算符(operator)的麻烦

标签 java operators

问题是关于我的 if 语句。我正在比较三个相同类型的值,但出现这样的错误“The argument type == is undefined for types boolean,int” 问题是,如果我更改代码以使用 && 分别比较值..所以 value == value1 && value = value2,我没有收到错误。有什么区别?

Card[] cardsIn = cards;
    boolean threeOfAKindEval = false;
    //the for loops runs through every possible combination of the 5 card array.  It starts at
    //0,0,0 and ends at 5,5,5/  Inside  the last for loop, I check for a three of a kind
    //My if statement also checks to make sure I am not comparing the same card with
    //itself three times 
    for(int index = 0; index < cards.length; index++){
        for(int indexCheck = 0; indexCheck < cards.length;indexCheck++){
            for(int indexCheckThree = 0; indexCheckThree < cards.length; 
                    indexCheckThree++){
                if(cardsIn[index].getValue() == cardsIn[indexCheck].getValue()  == cardsIn[indexCheckThree].getValue())
                    threeOfAKindEval = true;
            }
        }
    }

最佳答案

== 比较两个相同类型的参数并返回 boolean 结果。

  cardsIn[index].getValue() == cardsIn[indexCheck].getValue()  == cardsIn[indexCheckThree].getValue())

评估为

  bool temporalBool = cardsIn[indexCheck].getValue()  == cardsIn[indexCheckThree].getValue())
  bool finalBool = cardsIn[indexCheck].getValue()  == temporalBool // <-- left side int, right side bool

&& 运算符执行 boolean 类型的逻辑 AND,因此它就是您所需要的。

 cardsIn[index].getValue() == cardsIn[indexCheck].getValue()  && cardsIn[index].getValue() == cardsIn[indexCheckThree].getValue()

评估为

 bool temporalBool1 = cardsIn[index].getValue() == cardsIn[indexCheckThree].getValue()
 bool temporalBool2 = cardsIn[index].getValue() == cardsIn[indexCheck].getValue()  
 bool result = temporalBool1 && temporalBool2

关于java - 三个运算符(operator)的麻烦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19893460/

相关文章:

java - 如何制作(静态)初始化程序 block strictfp?

java - 如何使用 Jbutton 执行具有 main 方法的类

java - Hibernate二级缓存并不是在所有情况下都使用

list - `::` 和 `+:` 在列表前面有什么区别)?

c++ - #在c++中定义一个特殊的运算符

java - Java 中仅将小数点四舍五入到最接近的十位,例如 1.17 到 1.20

java - 修改Java单元测试中多个bean中使用的参数值

java - 数学方程式在 Java 中是如何工作的?

c# - 猫王运算符(Nullsave Dereference Operator)是否导致空引用异常?

Java 和 python ^ 运算符