Java三元运算符(?:)不起作用;第二个或第三个操作数返回布尔值 [英] Java ternary operator (?:) doesn't work; second or third operand return boolean
本文介绍了Java三元运算符(?:)不起作用;第二个或第三个操作数返回布尔值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有人能告诉我为什么三元运算符的使用不正确吗?操作数2和3返回一个布尔值。
Can someone tell me why this use of the ternary operator is incorrect? Operands 2 and 3 return a boolean.
public class Something {
...
private static final double REFERENCE_FRAME_MID_X = 0;
private static final double REFERENCE_FRAME_MID_Y = 0;
private boolean findInsideOrOutsideGeneralEllipse(Point2D destCirclePos) {
List<Boolean> returnValue = new ArrayList<>();
Point2D referenceFrameCenter = new Point2D.Double(REFERENCE_FRAME_MID_X, REFERENCE_FRAME_MID_Y);
Ellipse2D insideAreaEllipse2D = getEllipse2D(referenceFrameCenter.getX(), referenceFrameCenter.getY(),
destCirclePos.distance(referenceFrameCenter));
// doesn't work
insideAreaEllipse2D.contains(destCirclePos) ? returnValue.add(true) : returnValue.add(false);
// works
if (insideAreaEllipse2D.contains(destCirclePos)) {
returnValue.add(true);
} else {
returnValue.add(false);
}
}
...
}
推荐答案
Java三元操作条件的使用应该类似于
Usage of Java ternary operation condition should looks like
result = testCondition ? value1 : value2
它是java语言规范。
it's java-language specification.
在以下示例中,
此运算符应该读作:如果 someCondition 是 true ,请将 value1 的
值分配给结果。否则,赋值 value2 到
结果
In the following example, this operator should be read as: "If someCondition is true, assign the value of value1 to result. Otherwise, assign the value of value2 to result
这篇关于Java三元运算符(?:)不起作用;第二个或第三个操作数返回布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文