1. 程式人生 > >第1章 選擇流程控制語句

第1章 選擇流程控制語句

Java

第1章 選擇流程控制語句

1.1 順序結構的基本使用1.1.1 順序結構概述
是程序中最簡單最基本的流程控制,沒有特定的語法結構,按照代碼的先後順序,依次執行,程序中大多數的代碼都是這樣執行的
1.1.2 順序流程流程圖

1.1.3 代碼案例一

package com.itheima_01;

/*
 * 順序結構:從上往下,依次執行
 */
public class OrderDemo {
        public static void main(String[] args) {
                System.out.println("開始");
                System.out.println("語句A");
                System.out.println("語句B");
                System.out.println("語句C");
                System.out.println("結束");
        }
}v

1.2 If語句的格式1.2.1 If語句格式1及執行流程
if語句第一種格式:
if(關系表達式) {
語句體
}
執行流程:
首先判斷關系表達式看其結果是true還是false
如果是true就執行語句體
如果是false就不執行語句體
1.2.2 if語句格式1執行流程圖

1.2.3 代碼案例二

package com.itheima_02;
/*
 * if語句有三種格式。
 *
 * if語句格式1:
 *                 if(關系表達式) {
 *                         語句體;
 *                 }
 *
 * 執行流程:
 *                 A:首先判斷關系表達式看其結果是true還是false
 *                 B:如果是true,就執行語句體
 *                 C:如果是false,就不執行語句體
 */
public class IfDemo {
        public static void main(String[] args) {
                System.out.println("開始");
                // 定義兩個變量
                int a = 10;
                int b = 20;

                if (a == b) {
                        System.out.println("a等於b");
                }

                int c = 10;
                if (a == c) {
                        System.out.println("a等於c");
                }

                System.out.println("結束");
        }
}

1.2.4 If語句格式2及執行流程
if語句第二種格式:
if(關系表達式) {
語句體1;
} else {
語句體2;
}
執行流程
首先判斷關系表達式看其結果是true還是false
如果是true就執行語句體1
如果是false就執行語句體2
1.2.5 if語句格式2執行流程圖

1.2.6 代碼案例三

package com.itheima_02;
/*
 * if語句格式2:
 *                 if(關系表達式) {
 *                         語句體1;
 *                 }else {
 *                         語句體2;
 *                 }
 *
 * 執行流程:
 *                 A:判斷關系表達式的值是true還是false
 *                 B:如果是true,就執行語句體1
 *                 C:如果是false,就執行語句體2
 */
public class IfDemo2 {
        public static void main(String[] args) {
                System.out.println("開始");
                // 判斷給定的數據是奇數還是偶數
                // 定義變量
                int a = 100;
                // 給a重新賦值
                a = 99;

                if (a % 2 == 0) {
                        System.out.println("a是偶數");
                } else {
                        System.out.println("a是奇數");
                }

                System.out.println("結束");
        }
}

1.2.7 If語句格式3及執行流程
if語句第三種格式:
if(關系表達式1) {
語句體1;
}else if (關系表達式2) {
語句體2;
}

else {
語句體n+1;
}
執行流程
首先判斷關系表達式1看其結果是true還是false
如果是true就執行語句體1
如果是false就繼續判斷關系表達式2看其結果是true還是false
如果是true就執行語句體2
如果是false就繼續判斷關系表達式…看其結果是true還是false

如果沒有任何關系表達式為true,就執行語句體n+1
1.2.8 if語句格式3執行流程圖

1.2.9代碼案例四

package com.itheima_02;

/*
 * if語句格式3:
 *                 if(關系表達式1) {
 *                         語句體1;
 *                 }else if(關系表達式2) {
 *                         語句體2;
 *                 }else if(關系表達式3) {
 *                         語句體3;
 *                 }
 *                 ...
 *                 else {
 *                         語句體n+1;
 *                 }
 *
 * 執行流程:
 *                 A:首先判斷關系表達式1看其結果是true還是false
 *                 B:如果是true,就執行語句體1
 *                      如果是false,就繼續進行關系表達式2的判斷看其結果是true還是false
 *                 C:如果是true,就執行語句體2
 *                    如果是false,就繼續進行關系表達式...的判斷看其結果是true還是false
 *                 ...
 *                 D:如果沒有一個為true的,就執行語句體n+1
 *
 * if語句的三種格式:
 *                 第一種格式適合做一種情況的判斷
 *                 第二種格式適合做二種情況的判斷
 *                 第三種格式適合做多種情況的判斷
 */
public class IfDemo3 {
        public static void main(String[] args) {
                // x和y的關系滿足如下:
                // x>=3 y = 2x + 1;
                // -1<=x<3 y = 2x;
                // x<=-1 y = 2x – 1;
                // 根據給定的x的值,計算出y的值並輸出。

                // 定義變量
                int x = 5;

                /*
                int y;
                if (x >= 3) {
                        y = 2 * x + 1;
                } else if (x >= -1 && x < 3) {
                        y = 2 * x;
                } else if (x <= -1) {
                        y = 2 * x - 1;
                }else {
                        y = 0;
                }
                */

                int y = 0;
                if (x >= 3) {
                        y = 2 * x + 1;
                } else if (x >= -1 && x < 3) {
                        y = 2 * x;
                } else if (x <= -1) {
                        y = 2 * x - 1;
                }

                System.out.println("y的值是:"+y);
        }
}

1.3 If語句的練習1.3.1 if語句實現獲取兩個數據較大值1.3.2 代碼案例五

package com.itheima_02;

import java.util.Scanner;

/*
 * 鍵盤錄入兩個數據,獲取這兩個數據的較大值
 *
 * 分析:
 *                 A:看到鍵盤錄入,我們就應該想到鍵盤錄入的三步驟
 *                         導包,創建對象,接收數據
 *                 B:獲取這兩個數據的較大值,其實就是判斷兩個數據誰大,把大的輸出就可以了。
 *
 * 導包:
 *                 A:手動導包
 *                         import java.util.Scanner;
 *                 B:鼠標點擊紅色叉叉,自動生成
 *                 C:快捷鍵(推薦)
 *                         ctrl+shift+o
 */
public class IfTest {
        public static void main(String[] args) {
                //創建對象
                Scanner sc = new Scanner(System.in);

                //接收數據
                System.out.println("請輸入第一個數據:");
                int a = sc.nextInt();

                System.out.println("請輸入第二個數據:");
                int b = sc.nextInt();

                //采用if語句格式2實現
                /*
                if(a>b){
                        System.out.println("較大的值是:"+a);
                }else {
                        System.out.println("較大的值是:"+b);
                }
                */

                //拿到較大的值之後,我未必想直接輸出,所以我們定義變量接收這個較大的值
                int max;
                if(a>b){
                        max = a;
                }else {
                        max = b;
                }
                //可能做其他的操作
                //max += 100;
                System.out.println("較大的值是:"+max);
        }
}

1.3.3 if語句實現判斷學生等級1.3.4 代碼案例六

package com.itheima_02;

import java.util.Scanner;

/*
 * 鍵盤錄入學生考試成績,請根據成績判斷該學生屬於哪個級別
 * 90-100        優秀
 * 80-90        好
 * 70-80        良
 * 60-70        及格
 * 60以下        不及格
 *
 * 分析:
 *                 A:鍵盤錄入學生考試成績
 *                         三步驟
 *                 B:通過簡單的分析,我們決定采用if語句格式3來實現
 *
 * 程序一定要考慮周全了。
 *                 安全數據
 *                 邊界數據
 *                 錯誤數據
 */
public class IfTest2 {
        public static void main(String[] args) {
                //創建對象
                Scanner sc = new Scanner(System.in);

                //接收數據
                System.out.println("請輸入學生的考試成績:");
                int score = sc.nextInt();

                //if語句格式3
                /*
                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 {
                        System.out.println("你的成績屬於不及格");
                }
                */

                //我們發現程序不夠健壯,加入錯誤數據的判斷
                if(score<0 || score>100){
                        System.out.println("你的成績是錯誤的");
                }else 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 {
                        System.out.println("你的成績屬於不及格");
                }
        }
}

1.4 switch語句的格式及執行流程1.4.1 格式解釋:
switch表示這是switch語句
表達式的取值:byte,short,int,char
JDK5以後可以是枚舉
JDK7以後可以是String
case後面跟的是要和表達式進行比較的值
語句體部分可以是一條或多條語句
break表示中斷,結束的意思,可以結束switch語句
default語句表示所有情況都不匹配的時候,就執行該處的內容,和if語句的else相似。
1.4.2 執行流程
首先計算出表達式的值
其次,和case依次比較,一旦有對應的值,就會執行相應的語句,在執行的過程中,遇到break就會結束。
最後,如果所有的case都和表達式的值不匹配,就會執行default語句體部分,然後程序結束掉。
1.4.3 switch語句執行流程圖

1.5 switch語句的練習1.5.1 switch語句實現根據數字輸出對應星期1.5.2 代碼案例七

package com.itheima_03;

import java.util.Scanner;

/*
 * switch語句格式:
 * switch(表達式) {
 *                 case 值1:
 *                         語句體1;
 *                         break;
 *                 case 值2:
 *                         語句體2;
 *                         break;
 *                 ...
 *                 default:
 *                         語句體n+1;
 *                         break;
 * }
 * 格式解釋:
 *                 表達式:byte,short,int,char
 *                         JDK5以後可以是枚舉
 *                         JDK7以後可以是字符串
 *                 case:就是要和表達式進行比較的值
 *                 break:表示中斷,結束的意思。
 *                 default:表示所有的情況都不匹配的時候,就執行語句體n+1。和if語句的else相似。
 * 執行流程:
 *                 A:計算出表達式的值
 *                 B:拿計算出來的值和case後面的值依次比較,一旦有對應的值,就執行該處的語句,在執行過程中,遇到 break,就結束。
 *                 C:如果所有的case都不匹配,就會執行default控制的語句,然後結束。
 * 需求:
 *                 根據鍵盤錄入的數值1,2,3,…7輸出對應的星期一,星期二,星期三…星期日。
 */
public class SwitchDemo {
        public static void main(String[] args) {
                //創建鍵盤錄入對象
                Scanner sc = new Scanner(System.in);

                //接收數據
                System.out.println("請輸入一個數字(1-7):");
                int weekday = sc.nextInt();

                //switch語句實現選擇
                switch(weekday) {
                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;
                }
        }
}

第1章 選擇流程控制語句