【发布时间】:2013-07-12 05:41:42
【问题描述】:
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 1)
if(direction == 0){
System.out.print('L');
} else {
System.out.print('R');
}
【问题讨论】:
-
在 Java 中,0 没有布尔表示。为此只能使用布尔表达式。
标签: java ternary-operator