Java基础10:三元运算符及小结
扩展赋值运算符
package com.fei.operator;
public class Demo07 {
public static void main(String[] args) {
int a = 10;
int b = 20;
a += b;
System.out.println(a);
a -= b;
System.out.println(a);
a *= b;
System.out.println(a);
a /= b;
System.out.println(a);
double c = (double) b;
double d = (double) a;
d = d / c;
System.out.println(d); //0.5
}
}
字符串连接符
package com.fei.operator;
public class Demo08 {
public static void main(String[] args) {
//字符串连接符
int a = 10;
int b = 20;
System.out.println("a+b=" + a+b); //a+b=1020
System.out.println("a+b=" + (a+b)); //a+b=30
System.out.println(a+b+""); //30
}
}
如果字符串在前面,后面会进行拼接;如果字符串在后面,前面依旧会进行运算
三元运算符
x :y ? z
如果 x==true,则结果为y;否则结果为z
package com.fei.operator;
public class Demo09 {
public static void main(String[] args) {
//三元运算符
int score = 50;
String flag = (score > 60) ? "及格": "不及格";
System.out.println(flag);
}
}
Java基础11:包机制
导入这个包下所有的类:import com.fei.base.*;