问题是关于我的 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/