条件运算符的使用
/**
*测试条件运算符 X?Y:Z X为true则输出Y 为false则输出Z
*/
public class TestOperator07{
public static void main(String[]args){
int score = 90;
String a = score<60?"不及格":"及格";
System.out.println(a);//结果为false所以输出 "及格"
//作用同if语句
if(score<60){
a="不及格";
}else{
a="及格";
}
int x=-100;
int flag = x > 0 ? 1 : (x == 0 ? 0 : -1);//条件运算符的嵌套使用
System.out.println(flag);
}
}