条件运算符也称为三元运算符。该运算符由三个操作数组成,用于评估布尔表达式。操作者的目标是决定;应将哪个值分配给变量。运算符写为:
variable x = (expression)? value if true: value if false
public class Test {
public static void main(String args[]) {
int a, b;
a = 10;
b = (a == 1) ? 20: 30;
System.out.println("Value of b is: " + b);
b = (a == 10) ? 20: 30;
System.out.println(“Value of b is: " + b);
}
}输出结果
Value of b is: 30 Value of b is: 20