执行结果截图:
代码:
public class TernaryOperator {
public static void main(String[] args) {
int a = 10;
int b = 20;
a+=b; //a = a+b
a-=b; //a = a-b
System.out.println(a);
//字符串连接符
System.out.println(a+b);
System.out.println(""+a+b); // 如果前面是字符串,后面的a和b都会自动转换为字符串,不会计算
System.out.println(a+b+""); // 如果后面跟字符串,前面的a和b不会自动转换字符串,会计算
boolean x = true;
String y = "ok";
String z = "not ok";
//?:是三目运算符,如果x==true,则结果为y,否则结果为z
System.out.println(x?y:z);
System.out.println(!x?y:z);
int score = 80;
String type = score < 60 ? "failing grade":"passing grade";
System.out.println(type);
score = 50;
type = score < 60 ? "failing grade":"passing grade";
System.out.println(type);
}
}