JavaSE基礎之選擇結構&循環結構(四)

文章目錄

(一)流程概述與順序結構
(二)選擇結構:單if語句
(三)選擇結構:標準if-else語句
(四)選擇結構:擴展if-else語句
(五)練習:用if語句實現考試成績劃分
(六)練習:用if語句替換三元運算符
(七)選擇結構:標準的switch語句
(八)選擇結構:穿透的switch語句
(九)循環結構:循環概述與基本組成部分
(十)循環結構:for循環
(十一)循環結構:while循環
(十二)循環結構:do-while循環
(十三)練習:用循環求出1-100之間的偶數的數量
(十四)三種循環的區別
(十五)循環控制:break語句
(十六)循環控制:continue語句
(十七)死循環
(十八)循環嵌套

(一)流程概述與順序結構

在一個程序執行的過程中,各條語句的執行順序對程序的結果是有直接影響的。也就是說,程序的流程對運行結果有直接的影響。所以,我們必須清楚每條語句的執行流程。而且,很多時候我們要通過控制語句的執行順序來實現我們要完成的功能。

// 順序結構
public class Demo01Sequence {
	public static void main(String[] args) {
		System.out.println("今天天氣不錯");
		System.out.println("挺風和日麗的");
		System.out.println("我們下午沒課");
		System.out.println("這的確挺爽的");
	}
}

在這裏插入圖片描述

(二)選擇結構:單if語句

// 單if語句
public class Demo02If {
	public static void main(String[] args) {
		System.out.println("今天天氣不錯,正在壓馬路……突然發現一個快樂的地方:網吧");
		int age = 19;
		if (age >= 18) {
			System.out.println("進入網吧,開始high!");
			System.out.println("遇到了一羣豬隊友,開始罵街。");
			System.out.println("感覺不爽,結賬走人。");
		}
		System.out.println("回家喫飯");
	}
}

在這裏插入圖片描述

(三)選擇結構:標準if-else語句

// 標準的if-else語句
public class Demo03IfElse {
	public static void main(String[] args) {
		int num = 666;
		
		if (num % 2 == 0) { // 如果除以2能夠餘數爲0,說明是偶數
			System.out.println("偶數");
		} else {
			System.out.println("奇數");
		}
	}
}

在這裏插入圖片描述

(四)選擇結構:擴展if-else語句

// x和y的關係滿足如下:
// 如果x >= 3,那麼y = 2x + 1;
// 如果-1 < x < 3,那麼y = 2x;
// 如果x <= -1,那麼y = 2x – 1;
public class test {

    public static void main(String[] args) {
        int x = -10;
        int y;
        if (x >= 3) {
            y = 2 * x + 1;
        } else if (x > -1) { //走到這裏的時候,x已經小於3了
            y = 2 * x;
        } else {             //前面所有情況都不滿足,才執行這個,也就是x<3並且x<-1,也就是x<-1
            y = 2 * x - 1;
        }
        System.out.println("結果是:" + y);
    }

}

在這裏插入圖片描述

(五)練習:用if語句實現考試成績劃分

public class Demo05IfElsePractise {
	public static void main(String[] args) {
		int score = 120;
		if (score >= 90 && score <= 100) {
			System.out.println("優秀");
		} else if (score >= 80 && score < 90) {
			System.out.println("好");
		} else if (score >= 70 && score < 80) {
			System.out.println("良");
		} else if (score >= 60 && score < 70) {
			System.out.println("及格");
		} else if (score >= 0 && score < 60) {
			System.out.println("不及格");
		} else { // 單獨處理邊界之外的不合理情況
			System.out.println("數據錯誤");
		}
	}
}

注意:這種情況下,使用擴展if-else語句效率要比單if語句效率高
因爲使用擴展if-else語句只要中了其中一條,下面的就都不執行了,而單if語句是無論如何都要全部走一遍的

(六)練習:用if語句替換三元運算符

// 題目:使用三元運算符和標準的if-else語句分別實現:取兩個數字當中的最大值
public class Demo06Max {
	public static void main(String[] args) {
		int a = 105;
		int b = 20;
		
		// 首先使用三元運算符
		// int max = a > b ? a : b;
		
		// 使用今天的if語句
		int max;
		if (a > b) {
			max = a;
		} else {
			max = b;
		}
		
		System.out.println("最大值:" + max);
	}
}

(七)選擇結構:標準的switch語句

public class Demo07Switch {
	public static void main(String[] args) {
		int num = 10;
		
		switch (num) {
			case 1:
				System.out.println("星期一");
				break;
			case 2:
				System.out.println("星期二");
				break;
			case 3:
				System.out.println("星期三");
				break;
			case 4:
				System.out.println("星期四");
				break;
			case 5:
				System.out.println("星期五");
				break;
			case 6:
				System.out.println("星期六");
				break;
			case 7:
				System.out.println("星期日");
				break;
			default:
				System.out.println("數據不合理");
				break; // 最後一個break語句可以省略,但是強烈推薦不要省略
		}
	}
}

在這裏插入圖片描述

(八)選擇結構:穿透的switch語句

switch語句使用的注意事項:

  1. 多個case後面的數值不可以重複。

  2. switch後面小括號當中只能是下列數據類型:
    基本數據類型:byte/short/char/int
    引用數據類型:String字符串、enum枚舉

  3. switch語句格式可以很靈活:前後順序可以顛倒,而且break語句還可以省略。
    “匹配哪一個case就從哪一個位置向下執行,直到遇到了break或者整體結束爲止。”

public class Demo08SwitchNotice {
	public static void main(String[] args) {
		int num = 2;
		switch (num) {
			case 1:
				System.out.println("你好");
				break;
			case 2:
				System.out.println("我好");
				// break;
			case 3:
				System.out.println("大家好");
				break;
			default:
				System.out.println("他好,我也好。");
				break;
		} 
	}
}

(九)循環結構:循環概述與基本組成部分

循環語句可以在滿足循環條件的情況下,反覆執行某一段代碼,這段被重複執行的代碼被稱爲循環體語句,當反覆執行這個循環體時,需要在合適的時候把循環判斷條件修改爲false,從而結束循環,否則循環將一直執行下去,形成死循環。

循環結構的基本組成部分,一般可以分成四部分:

  1. 初始化語句:在循環開始最初執行,而且只做唯一一次
  2. 條件判斷:如果成立,則循環繼續;如果不成立,則循環退出
  3. 循環體:重複要做的事情內容,若干行語句
  4. 步進語句:每次循環之後都要進行的掃尾工作,每次循環結束之後都要執行一次

(十)循環結構:for循環

public class Demo09For {
	public static void main(String[] args) {
		for (int i = 1; i <= 100; i++) { //100遍
			System.out.println("我錯啦!原諒我吧!" + i);
		}
		System.out.println("程序停止");
	}
}

在這裏插入圖片描述

(十一)循環結構:while循環

  • 標準格式

      while (條件判斷) {
      	循環體;
      }
    
  • 擴展格式

      初始化語句;
      while (條件判斷) {
      	循環體;
      	步進語句;
      }
    
public class Demo10While {
	public static void main(String[] args) {
		for (int i = 1; i <= 10; i++) {
			System.out.println("我錯啦!" + i);
		}
		System.out.println("=================");
		
		int i = 1; // 1. 初始化語句
		while (i <= 10) { // 2. 條件判斷
			System.out.println("我錯啦!" + i); // 3. 循環體
			i++; // 4. 步進語句
		}
	}
}

在這裏插入圖片描述

(十二)循環結構:do-while循環

  • 標準格式

      do {
      	循環體;
      } while (條件判斷);
    
  • 擴展格式

      初始化語句;
      do {
      	循環體;
      	步進語句;
      } while (條件判斷);
    
public class Demo11DoWhile {
	public static void main(String[] args) {
		for (int i = 1; i <= 10; i++) {
			System.out.println("原諒你啦!起來吧!地上怪涼!" + i);
		}
		System.out.println("===============");
		
		int i = 1; // 1. 初始化語句
		do {
			System.out.println("原諒你啦!起來吧!地上怪涼!" + i); // 3. 循環體
			i++; // 4. 步進語句
		} while (i <= 10); // 2. 條件判斷
	}
}

在這裏插入圖片描述

(十三)練習:用循環求出1-100之間的偶數的數量

題目:求出1-100之間的偶數和

思路:

  1. 既然範圍已經確定了是1到100之間,那麼我就從1、2、3……一直到100這麼多數字一個一個進行檢查
  2. 總共有100個數字,並非所有數字都能用
    必須要是偶數才能用,判斷(if語句)偶數:num % 2 == 0
  3. 需要一個變量,用來進行累加操作
public class Demo12HundredSum {
	public static void main(String[] args) {
		int sum = 0; // 用來累加
		
		for (int i = 1; i <= 100; i++) {
			if (i % 2 == 0) { // 如果是偶數
				sum += i;
			}
		}
		System.out.println("結果是:" + sum);
	}
}

(十四)三種循環的區別

  1. 如果條件判斷從來沒有滿足過,那麼for循環和while循環將會執行0次
    但是do-while循環會執行至少一次

  2. for循環的變量在小括號當中定義,只有循環內部纔可以使用
    while循環和do-while循環初始化語句本來就在外面,所以出來循環之後還可以繼續使用

public class Demo13LoopDifference {
	public static void main(String[] args) {
		for (int i = 1; i < 0; i++) {
			System.out.println("Hello");
		}
		// System.out.println(i); // 這一行是錯誤寫法!因爲變量i定義在for循環小括號內,只有for循環自己才能用。
		System.out.println("================");
		
		int i = 1;
		do {
			System.out.println("World");
			i++;
		} while (i < 0);
		// 現在已經超出了do-while循環的範圍,我們仍然可以使用變量i
		System.out.println(i); // 2
	}
}

(十五)循環控制:break語句

break關鍵字的用法有常見的兩種:

  1. 可以用在switch語句當中,一旦執行,整個switch語句立刻結束
  2. 還可以用在循環語句當中,一旦執行,整個循環語句立刻結束。打斷循環

關於循環的選擇,有一個小建議:凡是次數確定的場景多用for循環;否則多用while循環

public class Demo14Break {
	public static void main(String[] args) {
		for (int i = 1; i <= 10; i++) {
			// 如果希望從第4次開始,後續全都不要了,就要打斷循環
			if (i == 4) { // 如果當前是第4次
				break; // 那麼就打斷整個循環
			}
			System.out.println("Hello" + i);
		}
	}
}

(十六)循環控制:continue語句

continue關鍵字:一旦執行,立刻跳過當前次循環剩餘內容,馬上開始下一次循環

public class Demo15Continue {
	public static void main(String[] args) {
		for (int i = 1; i <= 10; i++) {
			if (i == 4) { // 如果當前是第4層
				continue; // 那麼跳過當前次循環,馬上開始下一次(第5層)
			}
			System.out.println(i + "層到了。");
		}
	}
}

(十七)死循環

永遠停不下來的循環,叫做死循環

死循環的標準格式:

while (true) {
	循環體
}
public class Demo16DeadLoop {
	public static void main(String[] args) {
		while (true) {
			System.out.println("I Love Java!");
		}
		
		// System.out.println("Hello");
	}
}

(十八)循環嵌套

public class Demo17LoopHourAndMinute {
	public static void main(String[] args) {
		for (int hour = 0; hour < 24; hour++) { // 外層控制小時

			for (int minute = 0; minute < 60; minute++) { // 內層控制小時之內的分鐘
				System.out.println(hour + "點" + minute + "分");
			}

		}
	}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章