可视化三元运算符

时间:2018-03-05 09:20:57

标签: java operators ternary-operator

有人可以解释这行代码

int num1;
int num2;
int num3;
int largest = ( (num1 > num2) ? ( (num1 > num3) ? num1 : num3) : ((num2 > num3) ? num2 : num3));

在IF-Else声明中,所以我能理解

1 个答案:

答案 0 :(得分:1)

SELECT * FROM A
UNION ALL
SELECT * FROM B WHERE COUNT(SELECT * FROM A) = 0;

相当于

int largest = ( (num1 > num2) ? ( (num1 > num3) ? num1 : num3) : ((num2 > num3) ? num2 : num3));

虽然可以通过以下方式更简单地实现:

int largest;
if (num1 > num2) {
    if (num1 > num3) {
       largest = num1;
    } else {
       largest = num3;
    }
} else {
    if (num2 > num3) {
       largest = num2;
    } else {
       largest = num3;
    }
}