三目条件运算符
语法格式:
x ? y : z
其中 x 为 boolean 类型表达式,先计算 x 的值,若为true,则整个运算的结果为表达式 y 的值,否则整个运算结果为表达式 z 的值。
/**
* 条件运算目
* @author DELL
*
*/
public class TestconditionalOperator {
public static void main(String[] args) {
int score = 80;
int x = -100;
if(score < 60) {
System.out.println("不及格");
}else {
System.out.println("及格");
}
//可以提升逻辑判断效率
String type = score<60 ? "不及格" : "及格";
System.out.println(type);
//三目操作符可以嵌套使用,但不建议
System.out.println(x>0 ? 1 : (x==0 ? 0 : -1) );
}
}