相关疑难解决方法(0)

三元运算符不工作

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');
Run Code Online (Sandbox Code Playgroud)

我试过它是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');
}
Run Code Online (Sandbox Code Playgroud)

java ternary-operator

3
推荐指数
3
解决办法
3247
查看次数

标签 统计

java ×1

ternary-operator ×1