class BitTest {
public static void main(String[] args) {
System.out.println("13 << 2 : " + (13 << 2));
System.out.println("-13 << 2 : " + (-13 << 2));
System.out.println("13 << 27 : " + (13 << 27));
System.out.println("13 << 28 : " + (13 << 28));
System.out.println("13 >> 2 : " + (13 >> 2));
System.out.println("-13 >> 2 : " + (-13 >> 2));
System.out.println("-13 >>> 2 : " + (-13 >>> 2));
//练习:如何交换两个int型变量的值
int m = 10;
int n = 20;
System.out.println("m = " + m + ", n = " + n);
//交换两个变量的值(重点)
//方式一:推荐!
//int temp = m;
//m = n;
//n = temp;
//方式二:使用有局限性:① 可能会超出int的范围 ② 数据类型的局限性
//m = m + n;//10 + 20
//n = m - n;//30 - 20
//m = m - n;//30 - 10
//方式三:使用有局限性:数据类型的局限性
m = m ^ n;
n = m ^ n;
m = m ^ n;
System.out.println("m = " + m + ", n = " + n);
}
}
3. 三元运算符
说明
格式: (条件表达式)? 表达式1 : 表达式2
说明1: ① 条件表达式的结果为boolean
② 如果条件表达式的结果为true,则返回表达式1。反之,如果条件表达式的结果为false,则返回表达式2
③ 表达式1和表达式2满足一致性。
说明2:三元运算符可以嵌套使用
说明3:凡是可以使用三元运算符的地方,都可以改写成if-else结构。反之,不成立。
凡是既可以使用三元运算符,又可以使用if-else结构的地方,建议使用三元运算符。因为执行效率高一些。
代码演示
class SanYuanTest {
public static void main(String[] args) {
//获取两个数的较大值
int m = 10;
byte n = 15;
int max = (m > n)? m : n;
System.out.println("较大值为:" + max);
//String s = 12;//编译不通过
String maxString = (m > n)? "m大" : "n大";
n = 10;
String maxString1 = (m > n)? "m大" : ((m == n)? "m和n相等" : "n大");
System.out.println(maxString);
System.out.println(maxString1);
//练习:获取三个数的最大值
int a = 10;
int b = 43;
int c = 5;
int max1 = (a > b)? a : b;
int max2 = (max1 > c)? max1 : c;//不建议: int max2 = (((a > b)? a : b) > c)? ((a > b)? a : b) : c;
System.out.println(max2);
}
}
class RandomTest {
public static void main(String[] args) {
//random():返回一个>= 0.0 且 < 1.0的double型值
double value = Math.random();
System.out.println(value);
//获取[0,100]范围内的随机数:
int score = (int)(Math.random() * 101); //[0,1) -> [0,101) -> [0,100]
System.out.println(score);
//获取[10,30]范围内的随机数:
int value1 = (int)(Math.random() * 21 + 10); // [0,1) -> [0,21) -> [10,31) ->[10,30]
//获取[a,b]范围内的随机数:(int)(Math.random() * (b - a + 1) + a)
}
}
5. switch-case结构
结构
switch(表达式){
case 常量1:
语句1;
// break;
case 常量2:
语句2;
// break;
… …
case 常量N:
语句N;
// break;
default:
语句;
// break;
}
说明
① switch中的表达式,可以是如下的一些数据类型:byte \ short \ char \ int \ 枚举类型(JDK5.0) \ String (JDK7.0)
② 根据switch中的表达式的值,依次匹配case中的常量。一旦匹配成功,则进入相应的case的执行语句中执行。并考虑执行后续
的case结构。直到遇到break或switch-case结构执行结束为止。
或
③ switch-case要想实现多选一的效果,需要使用break。
③ default 相当于if-else中的else结构。是可选的,位置也是灵活的
④ switch-case 实现的结构都可以转换为if-else。反之,不成立。
凡是既可以使用switch-case,又可以使用if-else结构的地方,建议使用switch-case。因为执行效率高一些。
代码演示
class SwitchTest {
public static void main(String[] args) {
int num = 2;
switch(num){
case 0:
System.out.println("Zero");
break;
case 1:
System.out.println("One");
break;
case 2:
System.out.println("Two");
break;//跳出当前的switch-case结构
case 3:
System.out.println("Three");
break;
case 4:
System.out.println("Four");
break;
default:
System.out.println("Other");
//break;
}
//##########################
String season = "summer1";
switch (season) {
case "spring":
System.out.println("春暖花开");
break;
case "summer":
System.out.println("夏日炎炎");
break;
case "autumn":
System.out.println("秋高气爽");
break;
case "winter":
System.out.println("冬雪皑皑");
break;
default:
System.out.println("季节输入有误");
break;
}
}
}