Java笔记第十课:三元运算符


三元运算符及小结


扩展赋值运算符

public class Dome1 {
   public static void main(String[] args) {
       int a = 30;
       int b = 50;
       a += b;//a = a + b
       System.out.println(a);//输出80
       int c = 60;
       int d = 30;
       c -= d;//c = c - b
       System.out.println(c);//输出30
       int f = 5;
       int e = 3;
       f*=e;//f = f * e
       System.out.println(f);//输出15
       int h = 20;
       int y = 5;
       h/=y;//h = h * y
       System.out.println(h);//输出4
       //字符串连接符 + string类型
       System.out.println(""+a+b);//前面加上("")string类型都会变成字符串 输出8050
       System.out.println(a+b+"");//后面加上("")string类型则不会变成字符串 输出130
?
  }
}
?

位运算符

x ? y = z 如果x为true,则结果为y,反之为z

public class Dome1 {
   public static void main(String[] args) {
       int score = 80;
       String b = score < 60 ?"不及格":"及格";
       System.out.println(b);//输出及格
?
       int score1 = 50;
       String c = score1 < 60 ?"不及格":"及格";
       System.out.println(c);//输出不及格
  }
?
  }
?