Tag: 三元运算符

三元运算符不工作

Netbeans说我的三元运算符不是声明。 怎么来的? int direction; direction = (Math.random() < 0.5) ? 0 : 1; // direction is either L or R (0 or 1) direction == 0 ? System.out.print('L') : System.out.print('R'); 我试过它是if / then / else对应的并且它工作正常: int direction; direction = (Math.random() < 0.5) ? 0 : 1; // direction is either L or R (0 or […]

Java三元运算符(?:)不起作用; 第二个或第三个操作数返回布尔值

有人能告诉我为什么三元运算符的使用不正确吗? 操作数2和3返回一个布尔值。 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 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)) […]

java三元运算符

有人可以解释为什么这段代码? Collection c = (5 == 5) ? new ArrayList() : new HashSet(); 产生以下编译器错误: 不兼容的条件操作数类型ArrayList和HashSet 由于我不理解的原因,以下修复了该问题 Collection c = (5 == 5) ? (Collection) new ArrayList() : new HashSet(); 我正在使用Java 1.4。

为什么这种方法不起作用? Java三元运算符

这段代码出了什么问题: void bark(boolean hamlet) { hamlet ? System.out.println(“To Bark.”) : System.out.println(“Not to Bark”); }

没有赋值的Java三元组

有没有办法在没有做任务的情况下进行java三元操作或假冒伪造? 我喜欢做一堆if / then / elses时简洁的三元代码看起来如何。 我希望能够基于布尔代数语句调用两个void函数之一。 就像是: (bool1 && bool2) ? voidFunc1() : voidFunc2(); 我的函数是返回类型为void ,所以如果有一种方法可以在赋值中伪造它以使其工作,那么我对此很好……我想看看如何做到这一点:)

三元运算符 – JAVA

有可能改变这个: if(String!= null) { callFunction(parameters); } else { // Intentionally left blank } ……给三元运营商?