天天看點

運算符和if-else\switch-case 3

知識點1:運算符

1. 邏輯運算符

運算符和if-else\switch-case 3
  • 說明
& &&  |  ||  ! ^

說明:
1. 邏輯運算符操作的都是boolean類型的變量
2. 邏輯運算符的運算結果也是boolean類型
           
  • 代碼示範
class LogicTest {
	public static void main(String[] args) {
		
		//& 和 &&相同點:當符号左邊是true時,都執行符号右邊的操作
		//不同點:
		//& : 當符号左邊是false時,繼續執行符号右邊的操作
		//&& : 當符号左邊是false時,不再執行符号右邊的操作
		//& 與 && 表達的都是且的關系。但是,在開發中我們使用&&的頻率高一些。
		boolean b1 = true;
		b1 = false;
		int n1 = 10;
		if(b1 & (n1++) > 0){
			System.out.println("今天天氣稍微悶熱一些~");
		}else{
			System.out.println("明天預計天氣稍微悶熱一些~");
		}

		System.out.println("n1 = " + n1);

		//#################################
		boolean b2 = true;
		b2 = false;
		int n2 = 10;
		if(b2 && (n2++) > 0){
			System.out.println("今天天氣稍微悶熱一些~");
		}else{
			System.out.println("明天預計天氣稍微悶熱一些~");
		}

		System.out.println("n2 = " + n2);
		

		//###############################
		//| 和 || 相同點:當符号左邊是false時,都執行符号右邊的操作
		//不同點:
		//| : 當符号左邊是true時,繼續執行符号右邊的操作
		//|| : 當符号左邊是true時,不再執行符号右邊的操作
		//| 與 || 表達的都是或的關系。但是,在開發中我們使用||的頻率高一些。
		boolean b3 = false;
		b3 = true;
		int n3 = 10;
		if(b3 | (n3++) > 0){
			System.out.println("今天天氣稍微悶熱一些~");
		}else{
			System.out.println("明天預計天氣稍微悶熱一些~");
		}

		System.out.println("n3 = " + n3);
		
		//###################

		boolean b4 = false;
		b4 = true;
		int n4 = 10;
		if(b4 || (n4++) > 0){
			System.out.println("今天天氣稍微悶熱一些~");
		}else{
			System.out.println("明天預計天氣稍微悶熱一些~");
		}

		System.out.println("n4 = " + n4);

	}
}
           

2. 位運算符

  • 說明
<<   >>   >>>  &  |  ^  ~

總結:
<< : 在一定範圍内,每左移一位,數值*2

>> : 在一定範圍内,每右移一位,數值 / 2

"過猶不及"

>>> : 不管是正數還是負數,右移之後,高位都補0

總結:我們在開發中使用位運算的機會不多。
具體說到如果使用的話,主要是為了提升運算的效率。


經典的面試題:
最高效的方式計算 2 * 8  

2 << 3
8 << 1
           
  • 舉例
    運算符和if-else\switch-case 3
  • 代碼示範
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);
	}
}
           

4. 運算符的優先級

運算符和if-else\switch-case 3

知識點2:流程控制

1. 流程控制介紹

運算符和if-else\switch-case 3

2. if-else結構

  • 結構
結構一:
if(條件表達式){
	執行代碼塊;
}


結構二:二選一
if(條件表達式){
	執行代碼塊1;
}
else{
	執行代碼塊2;
}

結構三:多選一

if(條件表達式1){
	執行代碼塊1;
}
else if (條件表達式2){
	執行代碼塊2;
}
   ……
else{
	執行代碼塊n;
}
           
  • 說明
1. if-else結構中的else是可選的。
2. 如果兩個條件表達式是“互斥”關系,則哪個寫在上面,哪個寫在下面都可以。
   如果兩個條件表達式是有交集的關系,誰上誰下是有差別的。根據題目要求看哪個應該是聲明在上面。
   如果兩個條件表達式是“包含”關系,通常将範圍小的聲明在範圍大的上面。

3. 如果執行語句結構中隻有一行執行語句,則執行語句所在的一對{}可以省略。
   但是,不建議大家省略!
           
  • 代碼示範
//針對結構一:
		int heartBeats = 80;
		//另外:if(heartBeats >= 60 && heartBeats <= 100) 不能寫成:if(60<=heartBeats<=100)
		if(heartBeats < 60 || heartBeats > 100){  
			System.out.println("需要做進一步的檢查");

		}

		System.out.println("體檢結束");
		
		//針對結構二:
		int age = 20;
		if(age < 18){
			System.out.println("還未成年,可以多看看卡通片");
		}else{
			System.out.println("可以看看動作片、戰争片、...");
		}

		//針對結構三:
		age = 150;
		if(age < 0){
			System.out.println("資料輸入有誤");
		}else if(age < 6){
			System.out.println("嬰幼兒時期");
		}else if(age < 13){
			System.out.println("少年時期");
		}else if(age < 35){
			System.out.println("青壯年時期");
		}else if(age < 55){
			System.out.println("中年時期");
		}else if(age < 140){
			System.out.println("老年時期");
		}else{
			System.out.println("恭喜你,成仙兒了~");
		}
           
  • 代碼示範
class IfTest1 {
	public static void main(String[] args) {
		//1. 從鍵盤擷取嶽小鵬的期末成績
		Scanner scanner = new Scanner(System.in);
		System.out.println("請輸入嶽小鵬的期末成績(0-100):");
		int score = scanner.nextInt();

		//2. 根據成績做if-else判斷
		if(score == 100){
			System.out.println("獎勵一輛BMW");
			System.out.println("獎勵一輛BMW");
		}else if(score > 80 && score <= 99){
			System.out.println("獎勵一台iphone xs max");
		}else if(score >= 60 && score <= 80){
			System.out.println("獎勵一個 iPad");
		}else{
			System.out.println("什麼獎勵也沒有");
		}

	}
}
           
  • 代碼示範
/*
大家都知道,男大當婚,女大當嫁。那麼女方家長要嫁女兒,當然要提出一定的條件:
高:180cm以上;富:财富1千萬以上;帥:是。
如果這三個條件同時滿足,則:“我一定要嫁給他!!!”
如果三個條件有為真的情況,則:“嫁吧,比上不足,比下有餘。”
如果三個條件都不滿足,則:“不嫁!”

*/
import java.util.Scanner;
class IfTest3 {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);

		System.out.println("請輸入你的身高(cm):");
		int height = scan.nextInt();

		System.out.println("請輸入你的财富值(機關:萬):");
		double wealth = scan.nextDouble();
		
		/*
		方式一:

		System.out.println("請告訴我你是否帥?(true/false):");
		boolean isHandsome = scan.nextBoolean();

		if(height > 180 && wealth > 1000 && isHandsome){
			System.out.println("我一定要嫁給他!!!");
			
		}else if(height > 180 || wealth > 1000 || isHandsome){
			System.out.println("嫁吧,比上不足,比下有餘。");
		}else{
			System.out.println("不嫁!");
		}

		*/
		System.out.println("請告訴我你是否帥?(是/否):");
		String isHandsome = scan.next();

		if(height > 180 && wealth > 1000 && "是".equals(isHandsome)){
			System.out.println("我一定要嫁給他!!!");
			
		}else if(height > 180 || wealth > 1000 || "是".equals(isHandsome)){
			System.out.println("嫁吧,比上不足,比下有餘。");
		}else{
			System.out.println("不嫁!");
		}
	}
}

           

3. Scanner的使用

//1.
import java.util.Scanner;
/*
需求:如何從控制台擷取常見資料類型的變量

實作:使用Scanner類及其方法即可。

步驟:
1. 導包: import java.util.Scanner;
2. 在main方法中建立Scanner的執行個體: Scanner scan = new Scanner(System.in);
3. 調用Scanner的相關方法,擷取不同類型的變量: next() \ nextInt() \ nextBoolean() \ ....


說明:
如果要求輸入的類型與使用者實際輸入的類型不比對的話,報異常:InputMismatchException
*/

class ScannerTest {
	public static void main(String[] args) {
		//2.
		Scanner scan = new Scanner(System.in);
		//3.
		//3.1 擷取String類型的變量
		System.out.println("請輸入你的姓名:");
		String name = scan.next();
		System.out.println("名字為:" + name);

		//3.2 擷取整型int型的變量
		//擷取其他整型:nextByte() / nextShort() / nextLong()
		System.out.println("請輸入你的年齡:");
		int age = scan.nextInt();
		System.out.println("年齡為:" + age);

		//3.3 擷取浮點型double型的變量
		//擷取其他浮點型:nextFloat()
		System.out.println("請輸入你的體重:");
		double weight = scan.nextDouble();
		System.out.println("體重為:" + weight);

		//3.4 擷取boolean類型的變量
		System.out.println("請輸入你是否已婚(true/false):");
		boolean isMarried = scan.nextBoolean();
		System.out.println("是否已婚:" + isMarried);

		//3.5 擷取char類型的變量
		System.out.println("請輸入你的性别(男/女):");
		String gender = scan.next();//"男"
		char charGender = gender.charAt(0);//charAt(index):擷取index位置的字元
		System.out.println("性别為:" + charGender);

	}
}

           

4. 擷取随機數

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;
		}

		
	}
}