条 --X为true,则返回Y
件 --
运 -------- X ? Y : Z
算 --
符 -- X为false,则返回Z
另外一个方法:
String type = score<60? "不及格":"合格";
/**
测试条件运算符的作用
*/
public class TestOperator{
public static void main(String[] args){
int score = 90;
String a = score<60?"不及格":"及格";
System.out.println(a);
if(score<60){
a = "不及格";
}else{
a = "及格";
}
int x = -100;
int flag = x > 0 ? 1 : (x == 0 ? 0 : -1);
System.out.println(flag);//结果为-1
}
}