標籤:for迴圈 類型 迴圈 相等 def 局部變數 而且 變數類型 put
Java 基本文法---流程式控制制0. 概述
三大流程式控制制語句:順序、選擇、迴圈。
選擇結構:
- if 結構,if - else結構;
- 多重 if - else 語句 ;
- 嵌套 if - else 語句 ;
- switch 結構 ;
迴圈結構:
- while 迴圈, do - while 迴圈, for迴圈 ;
- Java增強for迴圈
- 迴圈嵌套
1. 選擇結構1.1 if 結構
一個if語句包含一個布林運算式和一條或多條執行語句;
布林運算式值為true,執行if 語句;
格式:
if(布林運算式){ //布林運算式值為true,執行語句;}
1.2 if - else 結構
布林運算式值為true,執行 if 語句;
布林運算式值為false,執行 else 語句;
格式:
if(布林運算式){ //布林運算式值為true,執行語句;}else{ //布林運算式值為false,執行語句;}
1.3 多重 if - else 結構
格式:
if(布林運算式1) { //布林運算式1值為true,執行語句;}else if(布林運算式2){ //布林運算式2值為true,執行語句;}else if(布林運算式3){ //布林運算式值3為true,執行語句;}else{ //如果以上所有運算式的值都為false,則執行語句;}
1.4 if 嵌套結構
格式:
if(布林運算式1){ //布林運算式1值為true,執行語句 if(布林運算式2) { //布林運算式2值為true,執行語句 }}
1.5 switch結構
格式:
switch(常量值/expression){ case value1: //執行語句 break; //可選 case value2: //執行語句 break; //可選 ...... default : //執行語句}
switch語句有如下規則:
- switch語句中的變數類型只能為byte、short、int或者char。
- switch語句可以擁有多個case語句。每個case後面跟一個要比較的值和冒號。
- case語句中的值的資料類型必須與變數的資料類型相同,而且只能是常量或者字面常量。
- 當變數的值與case語句的值相等時,那麼case語句之後的語句開始執行,直到break語句出現才會跳出switch語句。
- 當遇到break語句時,switch語句終止。程式跳轉到switch語句後面的語句執行。case語句不必須要包含break語句。如果沒有break語句出現,程式會繼續執行下一條case語句,直到出現break語句。
- switch語句可以包含一個default分支,該分支必須是switch語句的最後一個分支。default在沒有case語句的值和變數值相等的時候執行。default分支不需要break語句。
1.6 if 和 switch 區別
if 結構
- 判斷條件為布爾類型(布林運算式)
- 判斷條件是一個範圍
switch 結構
1.7 案例1--輸出九九乘法表
public class MultiplicationTable { public static void main(String[] args) { for (int i = 1; i <= 9; ++i) { for (int j = 1; j <= 9; j++) { if(j < i) { //輸出的空格由"%d * %d = %2d "決定 System.out.print(" "); } else { System.out.printf("%d * %d = %2d ", i ,j , i*j); } } System.out.println(); } }}Output:1 * 1 = 1 1 * 2 = 2 1 * 3 = 3 1 * 4 = 4 1 * 5 = 5 1 * 6 = 6 1 * 7 = 7 1 * 8 = 8 1 * 9 = 9 2 * 2 = 4 2 * 3 = 6 2 * 4 = 8 2 * 5 = 10 2 * 6 = 12 2 * 7 = 14 2 * 8 = 16 2 * 9 = 18 3 * 3 = 9 3 * 4 = 12 3 * 5 = 15 3 * 6 = 18 3 * 7 = 21 3 * 8 = 24 3 * 9 = 27 4 * 4 = 16 4 * 5 = 20 4 * 6 = 24 4 * 7 = 28 4 * 8 = 32 4 * 9 = 36 5 * 5 = 25 5 * 6 = 30 5 * 7 = 35 5 * 8 = 40 5 * 9 = 45 6 * 6 = 36 6 * 7 = 42 6 * 8 = 48 6 * 9 = 54 7 * 7 = 49 7 * 8 = 56 7 * 9 = 63 8 * 8 = 64 8 * 9 = 72 9 * 9 = 81
1.8 案例2--輸出某一年的某一天有多少天
public class Days { public static void main(String[] args) { int days = 0; Scanner sc = new Scanner(System.in); System.out.println("請輸入要確定的年份:"); int year = sc.nextInt(); System.out.println("請輸入要確定的月份:"); int month = sc.nextInt(); switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: days = 31; break; case 4: case 6: case 9: case 11: days = 30; break; case 2: if((year %400 == 0) || (year % 100 != 0 && year %4 == 0)) { days = 29; break; } else { days = 28; break; } default: System.out.println("您輸入的月份有誤!"); System.exit(0); } System.out.printf("%4d 年 %2d 月 共有 %2d 天\n",year,month,days); }}Output:請輸入要確定的年份:2018請輸入要確定的月份:022018 年 2 月 共有 28 天請輸入要確定的年份:2008請輸入要確定的月份:022008 年 2 月 共有 29 天
2.迴圈結構2.1
while迴圈
只要布林運算式值為true,就會執行迴圈內容。直到布林運算式值為false,退出迴圈;
while(布林運算式){ //布林運算式值為true,執行迴圈內容}
2.2
do ... while 迴圈
只要布林運算式值為true,就會執行迴圈內容。直到布林運算式值為false,退出迴圈;和while類似,不同的是do...while語句至少會被執行一次;
do{ //布林運算式值為true,執行迴圈內容}while(布林運算式)
2.3
for迴圈
for迴圈執行次數在執行前確定。
for(初始化;布林運算式;更新){ //執行代碼}
關於for的幾點說明:
- 最先執行初始化步驟。可以聲明一種類型,但可初始化一個或多個迴圈控制變數,也可以是空語句。
- 然後,檢測布林運算式的值。如果為true,迴圈體被執行。如果為false,迴圈終止,開始執行迴圈體後面的語句。
- 執行一次迴圈後,更新迴圈控制變數。
- 再次檢測布林運算式。迴圈執行上面的過程。
2.4 Java增強for迴圈
聲明語句:聲明新的局部變數,該變數的類型必須和數組元素的類型匹配。其範圍限定在迴圈語句塊,其值與此時數組元素的值相等。
運算式:運算式是要訪問的數組名,或者是傳回值為數組的方法。
for(聲明語句:運算式){ //執行代碼}
2.5 案例1--迴圈輸出26個英文小寫字母,要求分兩行輸出
public class OutputLetters { public static void main(String[] args) { //迴圈輸出26個英文小寫字母,要求分兩行輸出 char ch = 'a'; int count = 0;//控制換行 while(ch <= 'z') { System.out.printf(ch + " "); ch ++; count ++; if (count % 13 == 0) { System.out.println(); } } System.out.println(); ch = 'a'; count = 0; for(count = 1, ch = 'a';ch <= 'z';ch ++,count ++) { System.out.printf(ch + " "); if (count % 13 == 0) { System.out.println(); } } }}Output:a b c d e f g h i j k l m n o p q r s t u v w x y z a b c d e f g h i j k l m n o p q r s t u v w x y z
2.6
break
break語句
break語句可以結束當前迴圈的執行;
- 執行完
break語句,迴圈體中位於break語句後面的語句就不會被執行;
- 在多重迴圈嵌套中,
break語句相當於向外跳一層;
2.7
continue
continue語句:
continue語句只能用在迴圈裡;
continue語句可以結束當前迴圈的執行,但是要繼續下一次迴圈的執行;
public class OutputLettersDemo { public static void main(String[] args) { //迴圈輸出26個英文小寫字母,要求分兩行輸出 //練習break,cotinue char ch = 'a'; int count = 0;//控制換行 while(ch <= 'z') { if(ch == 'x') break; System.out.printf(ch + " "); ch ++; count ++; if (count % 13 == 0) { System.out.println(); } } System.out.println(); ch = 'a'; count = 0; for(count = 1, ch = 'a';ch <= 'z';ch ++,count ++) { if(ch == 'x') continue; System.out.printf(ch + " "); if (count % 13 == 0) { System.out.println(); } } }}Output:a b c d e f g h i j k l m n o p q r s t u v w a b c d e f g h i j k l m n o p q r s t u v w y z
從上面的例子可以看出,break語句直接退出當層迴圈(到x直接退出,不再輸出),而continue語句只是結束當前迴圈,並沒有退出(只是沒有輸出x)。
Java 基本文法---流程式控制制