流程式控制制對於任何一門編程語言來說都是至關重要的,它提供了控製程序步驟的基本手段。如果沒有流程式控制制語句,整個程式將按照線性的順序來執行,不能根據用戶的輸入決定執行的序列。 1.順序結構 Java程式是從上到下逐行執行語句,中間沒有任何判斷和跳轉。 2.分支結構(選擇語句) 根據條件,選擇性地執行某段代 ...
流程式控制制對於任何一門編程語言來說都是至關重要的,它提供了控製程序步驟的基本手段。如果沒有流程式控制制語句,整個程式將按照線性的順序來執行,不能根據用戶的輸入決定執行的序列。
1.順序結構
Java程式是從上到下逐行執行語句,中間沒有任何判斷和跳轉。
2.分支結構(選擇語句)
- 根據條件,選擇性地執行某段代碼。
- 有
if…else
和switch-case
兩種分支語句。
2.1 if……else
2.1.1 基本語法
-
單分支條件判斷:if
-
格式:
if(條件表達式){ 代碼塊; //如果條件表達式的值為true,將執行這部分語句 }
-
說明: 條件表達式必須是布爾表達式(關係表達式或邏輯表達式)或布爾變數。
-
執行流程:
-
首先判斷條件表達式的結果是true還是false;
-
如果是true就執行代碼塊;
-
如果是false就不執行代碼塊;
-
-
-
雙分支條件判斷:if……else
-
格式:
if(布爾表達式){ 代碼塊1; //如果布爾表達式的值為true,執行代碼塊1 }else{ 代碼塊2; //如果布爾表達式的值為false,執行代碼塊2 }
-
執行流程:
- 首先判斷條件表達式看其結果是true還是false
- 如果是true就執行代碼塊1
- 如果是false就執行代碼塊2
-
-
多分支條件判斷:**if……else if……else if **
-
格式:
if(布爾表達式 1){ //如果布爾表達式 1的值為true執行代碼 }else if(布爾表達式 2){ //如果布爾表達式 2的值為true執行代碼 }else if(布爾表達式 3){ //如果布爾表達式 3的值為true執行代碼 } …… else { //如果以上布爾表達式都不為true執行代碼 }
-
說明:
- if 語句至多有 1 個 else 語句,else 語句在所有的 else if 語句之後。
- if 語句可以有若幹個 else if 語句,它們必須在 else 語句之前。
- 一旦其中一個 else if 語句檢測為 true,其他的 else if 以及 else 語句都將跳過執行。
- 如果沒有任何關係表達式為true,就執行else中的語句塊,然後結束當前多分支。
-
2.1.2 應用舉例
案例1:成年人心率的正常範圍是每分鐘60-100次。體檢時,如果心率不在此範圍內,則提示需要做進一步的檢查。
int heartRate = 120; //聲明心跳次數變數並賦值
//單分支條件判斷:if的練習使用
if(heartRate >= 60 || heartRate <= 100) {
System.out.println("心率不正常,需要做進一步的檢查");
}
System.out.println("檢測結束");
案例2:定義一個整數,判定是偶數還是奇數
public class IfElseExer02 {
public static void main(String[] args) {
int a = 2;//定義一個整數變數a,並賦值為2
//使用if……else,判斷一個整數的奇偶
if(a % 2 == 0) {
System.out.println("a是偶數");
}else {
System.out.println("a是奇數");
}
}
}
案例3:
岳小鵬參加Java考試,他和父親岳不群達成承諾。如果:成績為100分時,獎勵一輛跑車;成績為(80,99]時,獎勵一輛山地自行車;當成績為[60,80]時,獎勵環球影城一日游;其它時,胖揍一頓。
說明:預設成績是在[0,100]範圍內
public class IfElseExer03 {
public static void main(String[] args) {
int grade = 66; //聲明成績變數,並賦值為66
//使用多分支條件判斷:if……else if……else來完成案例3
if(grade == 100) {
System.out.println("獎勵一輛跑車");
}else if(grade >80 && grade <= 99) {
System.out.println("獎勵一輛山地自行車");
}else if(grade >= 60 && grade <= 80) {
System.out.println("獎勵環球影城一日游");
}else {
System.out.println("胖揍一頓");
}
}
}
2.1.3 if……else嵌套
在 if 的語句塊中,或者是在else語句塊中,又包含了另外一個條件判斷(可以是單分支、雙分支、多分支),就構成了嵌套結構
。
執行的特點:
(1)如果是嵌套在if語句塊中的,只有當外部的if條件滿足,才會去判斷內部的條件
(2)如果是嵌套在else語句塊中的,只有當外部的if條件不滿足,進入else後,才會去判斷內部的條件
if(布爾表達式 1){
//如果布爾表達式 1的值為true執行代碼
if(布爾表達式 2){
//如果布爾表達式 2的值為true執行代碼
}
}
//if……else嵌套的小練習
public class IfElseExer04 {
public static void main(String[] args) {
int x = 30;
int y = 10;
if(x == 30) {
if(y == 10) {
System.out.println("X = 30 "+"Y = 10");
}
}
}
}
if……else嵌套語句的案例小練習:
/*
由鍵盤輸入三個整數分別存入變數num1、num2、num3,對它們進行排序(使用 if-else if-else),並且從小到大輸出。
*/
public class IfElseExer05 {
public static void main(String[] args) {
//使用Scanner類接收三個整數並分別賦值給變數num1、num2、num3
System.out.println("請輸入3個整數:");
Scanner sc = new Scanner(System.in);
int num1 = sc.nextInt();
int num2 = sc.nextInt();
int num3 = sc.nextInt();
//使用if……else嵌套語句實現判斷輸入的三個整數大小,並按從小到大的排序列印
if(num2 >= num1) {
if(num3 >= num2) {
System.out.println(num1+"<"+num2+"<"+num3);
}else if(num3 <= num1) {
System.out.println(num3+"<"+num1+"<"+num2);
}else {
System.out.println(num2+"<"+num3+"<"+num1);
}
}else { //num2 <= num1
if(num3 >= num1) {
System.out.println(num2+"<"+num1+"<"+num3);
}else if(num3 <= num2) {
System.out.println(num3+"<"+num2+"<"+num1);
}else {
System.out.println(num2+"<"+num3+"<"+num1);
}
}
sc.close();
}
}
2.2 switch-case分支(選擇)結構
2.2.1 基本語法
-
語法格式
switch(expression) { case value : //執行語句1 break; //可選 case value : //執行語句2 break; //可選 …… //你可以有任意數量的case語句 default : //可選 //語句 }
-
執行流程圖:
-
執行過程:
-
根據switch中表達式的值,依次匹配各個case。如果表達式的值等於某個case中的常量值,則執行對應case中的執行語句。
-
執行完此case的執行語句以後,
-如果遇到break,則執行break並跳出當前的switch-case結構
-如果沒有遇到break,則會繼續執行當前case之後的其它case中的執行語句。--->case穿透...
-直到遇到break關鍵字或執行完所有的case及default的執行語句,跳出當前的switch-case結構。
-
-
switch-case語句的使用有如下規則:
- switch 語句中的變數類型是: byte、short、int 、char。從 Java SE 7 開始,switch 支持字元串 String 類型了,同時 case 標簽必須為字元串常量或字面量。
- switch 語句可以擁有多個 case 語句。每個 case 後面跟一個要比較的值和冒號。
- case 語句中的值的數據類型必須與變數的數據類型相同,而且只能是常量或者字面常量。
- 當變數的值與 case 語句的值相等時,那麼 case 語句之後的語句開始執行,直到 break 語句出現才會跳出 switch 語句。
- 當遇到 break 語句時,switch 語句終止。程式跳轉到 switch 語句後面的語句執行。case 語句不必須要包含 break 語句。如果沒有 break 語句出現,程式會繼續執行下一條 case 語句,直到出現 break 語句。
- switch 語句可以包含一個 default 分支,該分支一般是 switch 語句的最後一個分支(可以在任何位置,但建議在最後一個)。default 在沒有 case 語句的值和變數值相等的時候執行。default 分支不需要 break 語句。
2.2.2 應用舉例
public class Test {
public static void main(String args[]){
char grade = 'C';
switch(grade) {
case 'A' :
System.out.println("優秀");
break;
case 'B' :
case 'C' :
System.out.println("良好");
break;
case 'D' :
System.out.println("及格");
break;
case 'F' :
System.out.println("差");
break;
default :
System.out.println("成績等級錯誤");
}
System.out.println("你的等級是 " + grade);
}
}
案例:
使用switch-case實現對學生成績大於60分的,輸出“合格”。低於60分的,輸出“不合格”。
public class SwitchCaseExer03 {
public static void main(String[] args) {
int grade = 61; //聲明成績變數,並賦值為61
//使用switch-case分支語句實現案例3
switch (grade / 60) {
case 0:
System.out.println("成績不合格");
break;
case 1:
System.out.println("及格");
break;
default:
System.out.println("成績格式錯誤");
}
}
}
2.2.3 switch-case中的case穿透特性
在switch語句中,如果case的後面不寫break,將出現穿透現象,也就是一旦匹配成功,不會在判斷下一個case的值,直接向後運行,直到遇到break或者default或者運行完所有case才終止,執行終止。
我們可以利用這個特性實現某些功能,或者解決某些問題。
案例:編寫程式:從鍵盤上輸入2023年的“month”和“day”,要求通過程式輸出輸入的日期為2023年的第幾天。
public class SwitchCaseExer04 {
public static void main(String[] args) {
//使用Scanner,從鍵盤接收2023年的月、日
Scanner scan = new Scanner(System.in);
System.out.println("請輸入當前月份:");
int month = scan.nextInt(); //阻塞式方法
System.out.println("請輸入日期:");
int day = scan.nextInt();
int sumDays = 0; //記錄總天數
//使用switch-case分支語句實現,通過程式輸出輸入的日期為2023年的第幾天
switch(month) {
case 1:
sumDays = day;
break;
case 2:
sumDays = day +30;
break;
case 3:
sumDays = day + 30+ 28;
break;
case 4:
sumDays = day + 30 + 28 + 31;
break;
case 5:
sumDays = day + 30 + 28 + 31 + 30;
break;
case 6:
sumDays = day + 30 + 28 + 31 + 30 + 31;
break;
case 7:
sumDays = day + 30 + 28 + 31 + 30 + 31 + 30;
break;
case 8:
sumDays = day + 30 + 28 + 31 + 30 + 31 + 30 + 31;
break;
case 9:
sumDays = day + 30 + 28 + 31 + 30 + 31 + 30 + 31 + 31;
break;
case 10:
sumDays = day + 30 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30;
break;
case 11:
sumDays = day + 30 + 28 + 31 + 30 + 31 + 30 + 31 + 31+ 30 + 31;
break;
case 12:
sumDays = day + 30 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30;
break;
}
System.out.println("當前日期為:2023年"+month+"月"+day+"日"+",日期為2023年的第"+sumDays+"天");
scan.close();
}
}
上面例子我們可知代碼中重覆寫了很多邊遍相同的數據,數據冗餘。我們可以利用case的穿透性解決這個問題如下:
public class SwitchCaseExer05 {
public static void main(String[] args) {
//使用Scanner,從鍵盤接收2023年的月、日
Scanner scan = new Scanner(System.in);
System.out.println("請輸入當前月份:");
int month = scan.nextInt(); //阻塞式方法
System.out.println("請輸入日期:");
int day = scan.nextInt();
int sumDays = 0; //記錄總天數
//使用switch-case分支語句實現,通過程式輸出輸入的日期為2023年的第幾天,利用了case穿透性
switch(month) {
case 12:
sumDays += 30;//這個30是代表11月份的滿月天數
case 11:
sumDays += 31;//這個31是代表10月份的滿月天數
case 10:
sumDays += 30;//這個30是代表9月份的滿月天數
case 9:
sumDays += 31;//這個31是代表8月份的滿月天數
case 8:
sumDays += 31;//這個31是代表7月份的滿月天數
case 7:
sumDays += 30;//這個30是代表6月份的滿月天數
case 6:
sumDays += 31;//這個31是代表5月份的滿月天數
case 5:
sumDays += 30;//這個30是代表4月份的滿月天數
case 4:
sumDays += 31;//這個31是代表3月份的滿月天數
case 3:
sumDays += 28;//這個28是代表2月份的滿月天數
case 2:
sumDays += 31;//這個31是代表1月份的滿月天數
case 1:
sumDays += day;//這個day是代表當月的第幾天
}
System.out.println("當前日期為:2023年"+month+"月"+day+"日"+",日期為2023年的第"+sumDays+"天");
scan.close();
}
}
3.迴圈結構(迴圈語句)
- 根據迴圈條件,重覆性的執行某段代碼。
- 有
for
、while
、do-while
三種迴圈語句。 - 根據迴圈條件,重覆性的執行某段代碼。
- 有
for
、while
、do-while
三種迴圈語句。 - 補充:JDK5.0 提供了
foreach
迴圈,方便的遍歷集合、數組元素。 - 迴圈結構的四個要素:
- 初始化條件
- 迴圈條件
- 迴圈體
- 迭代部分
3.1 while迴圈
3.1.1 基本語法
語法格式:
初始化部分
while(條件表達式) {
迴圈體;
迭代語句;
}
說明:
- while(迴圈條件)中迴圈條件必須是boolean類型;
- 註意不要忘記聲明迭代部分。否則,迴圈將不能結束,變成死迴圈。
- for迴圈與while迴圈的區別:初始化條件部分的作用域不同。
3.1.2 應用舉例
案例1:計算1~1000的相加結果
public class WhileExer02 {
public static void main(String[] args) {
//使用while迴圈計算1到1000的相加結果
int i = 1;
int sum = 0;
while(i <= 1000) {
sum += i;
i++;
}
System.out.println("1~1000的相加結果為:"+sum);
}
}
案例2:遍歷1-100的偶數,並計算所有偶數的和、偶數的個數(累加的思想)
public class WhileExer03 {
public static void main(String[] args) {
int i = 1;
int sum = 0; //聲明變數sum,用於存儲所有的偶數和,並初始化值為0
int count = 0; //聲明變數count,用於存儲偶數的個數,並初始化為0
//使用while迴圈,實現1~100的偶數遍歷,並計算所有偶數的和以及偶數的個數
while(i <= 100) {
if(i % 2 == 0){
System.out.println(i);
sum += i;
count++;
}
i++;
}
System.out.println("所有偶數的和為:"+sum+'\n'+"偶數的個數為:"+count);
}
}
3.3 do……while迴圈
3.3.1 基本語法
語法格式:
do {
迴圈體語句;
迭代語句;
}while(條件表達式);
說明:
- 結尾while(迴圈條件)中迴圈條件必須是boolean類型;
- do{}while();最後有一個分號;
- do-while結構的迴圈體語句是至少會執行一次,這個和for和while是不一樣的;
- do……while語句和while語句的區別:
- while迴圈語句是先判斷條件是否成立再執行迴圈體;
- do……while是先執行一次迴圈體後,再判斷條件是否成立,也就是說do……while語句”{}“中的程式段至少要執行一次。
3.3.2 應用舉例
案例1:遍歷1-100的偶數,並計算所有偶數的和、偶數的個數(累加的思想)
public class DoWhileExer {
public static void main(String[] args) {
int i = 1;
int sum = 0; //存偶數的和
int count = 0; //存偶數的個數
//使用do……while迴圈語句實現遍歷1-100的偶數,並計算所有偶數的和、偶數的個數(累加的思想)
do {
if(i % 2 == 0){
System.out.println(i);
sum += i;
count++;
}
i++;
}while(i <= 100);
System.out.println("sum = "+sum);
System.out.println("count = "+count);
}
}
3.4 for迴圈
3.4.1 基本語法
for(表達式1;表達式2;表達式3) {
迴圈體;
}
表達式1:初始化表達式,負責完成變數的初始化;
表達式2:迴圈條件表達式,值為boolean型表達式,指定迴圈條件。等同於while迴圈()里的表達式;
表達式3:每次迴圈結束後執行的語句,通常用來改變迴圈條件,迭代部分;
3.4.2 應用舉例
案例1:使用for迴圈重覆執行某些語句
題目:輸出5行HelloWorld
public class ForExer01 {
public static void main(String[] args) {
int i; //聲明變數i,用於存儲for迴圈中的初始化表達式的值
//使用for迴圈語句列印五次HelloWorld!
for(i = 1 ; i <= 5;i++) {
System.out.println("HelloWorld!");
}
}
}
案例2:利用輸出語句學習for迴圈語句的執行過程
public class ForExer02 {
public static void main(String[] args) {
int num = 1;
for(System.out.print("a"); num < 4; System.out.println("b"),num++) {
System.out.print("c");
}
System.out.println(); //換行
System.out.println("num = "+num);
}
}
總結:從運行結果我們可知這個例子中的for迴圈執行過程是:表達式1-->>表達式2(值為true)-->>迴圈體-->>表達式3-->>表達式2(true)-->>迴圈體-->>表達式3-->>表達式2(true)-->>迴圈體-->>表達式3-->>表達式2(false)-->>for迴圈結束。
案例3:使用for迴圈計算2~100以內所有的偶數的和
public class ForExer06 {
public static void main(String[] args) {
int sum = 0; //存偶數相加的和
for(int i = 2; i <= 100; i += 2){
sum += i;
}
System.out.println("所有偶數的和為:"+sum);
}
}
3.5 迴圈控制
迴圈控制包含兩方面的內容,一方面是控制迴圈變數的變化方式,另一方面是控制迴圈的跳轉。
控制迴圈的跳轉需要用到break和continue兩個關鍵字,這兩條跳轉語句的跳轉效果不同,break語句是中斷迴圈,continue語句是跳過此次迴圈執行下一次迴圈。
3.5.1 break
使用break語句可以跳出switch……case結構。在迴圈結構中,同樣可以用break語句跳出當前迴圈體,從而中斷當前迴圈。
//輸出1~20出現的第一個偶數
//for迴圈使用break語句跳出迴圈
public class BreakExer01 {
public static void main(String[] args) {
for(int i = 1; i <=20; i++) {
if(i % 2 == 0) {
System.out.println("1~20出現的第一個偶數:"+i);
break;
}
}
}
}
3.5.2 continue
continue語句是針對break語句的補充。continue不是立即跳出迴圈體,而是跳過本次迴圈,回到迴圈的條件判斷部分,重新開始執行迴圈。在for迴圈中遇到continue後,首先執行迴圈的增量部分。然後進行條件判斷。在while和do……while迴圈中,continue語句使控制直接回到條件判斷部分。
// 編寫一個for迴圈從1迴圈至20,如果當前迴圈的次數為偶數,則使用continue語句跳過迴圈
public class Continue01 {
public static void main(String[] args) {
for(int i = 1; i <= 20; i++) {
if(i % 2 == 0) {
continue;
}
System.out.println(i);
}
}
}