天天看點

第三天:流程控制語句

作者:無聊看這

day03流程控制語句

學習目标:

  • 了解流程控制語句的分類
    • 順序結構
    • 分支結構/選擇結構
    • 循環結構
  • 熟悉熟悉結構執行流程
    • 從上到下 從左到右
  • 掌握分支結構
    • if語句
      • 格式1 if(關系表達式){語句體}
      • 格式2 if(關系表達式){語句體1}else{語句體2}
      • 格式3 if(關系表達式1){語句體1} else if(關系表達式2){語句體2}else{語句體2}
    • switch語句
      • 格式 : switch(值){ case 值: 語句體;break; ....... default 語句體n+1;}
  • 循環結構
    • 掌握for循環
      • for(初始化語句;條件判斷語句;條件控制語句){循環體語句體}
    • while循環
      • 初始化語句 ; while(條件判斷語句){循環體語句; 條件控制語句}
    • do-while循環
      • 初始化語句 ; do{循環體語句;條件控制語句}while(條件判斷語句);
  • 死循環
    • for(;;){}
    • while(true){} 掌握
    • do{}while(true);
  • 熟悉流程跳轉控制語句
    • break
    • continue
  • 随機數
    • Random

1.流程控制語句

1.1 順序結構

/*
    順序結構:
        從上到下,從左到右,程式執行的基本順序
 */
public class Demo01 {
    public static void main(String[] args) {
        System.out.println("開始");
        System.out.println(1+2+3+4+5+"a"+6+7+8+9);
        System.out.println("b");
        System.out.println("c");
        System.out.println("結束");
    }
}           

1.2分支結構if語句

1.2.1 if語句格式1

import java.util.Scanner;

/*
    if語句
        格式1:
            if(關系表達式){
                語句體;
            }
        執行流程:
            1.判斷關系表達式的值
            2.如果為true執行語句體,否則執行其他代碼
 */
public class Demo01格式1 {
    public static void main(String[] args) {
        //鍵盤錄入一個數字判斷是否是偶數
        Scanner scanner = new Scanner(System.in);
        System.out.println("請輸入一個整數:");
        int num = scanner.nextInt();
        System.out.println("開始");
        //判斷是否是偶數
        if (num%2==0){
            System.out.println(num+"是偶數");
        }
        System.out.println("結束");
    }
}           

1.2.2 if語句格式2

/*
    if語句:
        格式2:
            if(關系表達式){
                語句體1;
            }else{
                語句體2;
            }
        執行流程:
            判斷關系表達式的值是否為true,如果為true執行語句體1,否則執行語句體2

 */
public class Demo02格式2 {
    public static void main(String[] args) {
        //鍵盤錄入一個數字判斷是奇數還是偶數
        Scanner scanner = new Scanner(System.in);
        System.out.println("請錄入一個數字:");
        int num = scanner.nextInt();
        //判斷奇偶數
        if (num%2==0){
            System.out.println(num+"是偶數");
        }else {
            System.out.println(num+"是奇數");
        }
    }
}           

1.2.3if語句格式3

/*
    if語句
        格式3:
            if(關系表達式1){
                語句體1
            }else if(關系表達式2){
                語句體2
            }else{
                語句體3
            }
         執行流程:
            1.先判斷關系表達式1的值
            2.如果為true執行 語句體1 結束
            3.如果為false 執行 關系表達式2
            4.判斷關系表達式2的值是否為true
            5.如果為true執行語句體2
            6.否則執行語句體3
 */
public class Demo03格式3 {
    public static void main(String[] args) {
        int a = 9;
        if (a>10){
            System.out.println("a大于10");
        }else if (a == 10){
            System.out.println("a等于10");
        }else {
            System.out.println("a小于10");
        }
    }
}
           

1.3分支語句switch語句

1.3.1 switch格式

import java.util.Scanner;

/*
    switch語句
        格式:
            switch(值){
                case 值: 語句體1;break;
                case 值: 語句體2;break;
                case 值: 語句體3;break;
                case 值: 語句體4;break;
                ... ...
                default:語句體n+1;break;
            }
         執行流程:
            擷取switch後面值和case語句後面的值進行比對,比對上就執行語句體,如果都不比對執行default語句
 */
public class Demo04switch {
    public static void main(String[] args) {
        //減肥計劃 鍵盤錄入星期數,檢視減肥計劃 周一 跑步 周二 遊泳 周三 動感單車 周四 爬山 周五 跳繩 周六 SPA 周日 吃頓好的
        //鍵盤錄入
        Scanner sc = new Scanner(System.in);
        System.out.println("請輸入你要檢視的星期數:");
        int week = sc.nextInt();
        //使用switch語句檢視減肥計劃
        switch (week){
            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("周六 SPA");break;
            case 7:System.out.println("周日 吃頓好的");break;
            default:
                System.out.println("您輸入的星期數不存在!");
        }
    }
}           

1.3.2 switch語句練習

/*
    鍵盤錄入一個是月份,輸出對應的季節

    分析:
        1.鍵盤錄入
            導包
            建立對象
            接收資料
        2.擷取接收的資料
            使用switch語句比對,輸出對應季節
        3.case語句具有穿透性,遇到break才停止
 */
public class Demo05switch練習 {
    public static void main(String[] args) {
        //建立對象
        Scanner scanner = new Scanner(System.in);
        //接收資料
        System.out.println("請輸入一個月份:");
        int month = scanner.nextInt();
        //使用switch語句比對,輸出對應季節
        switch (month) {
            case 12:
            case 1:
            case 2:
                System.out.println("冬季");
                break;
            case 3:
            case 4:
            case 5:
                System.out.println("春季");
                break;
            case 6:
            case 7:
            case 8:
                System.out.println("夏季");
                break;
            case 9:
            case 10:
            case 11:
                System.out.println("秋季");
                break;
            default:
                System.out.println("您給的月份有誤!");
                break;

        }

    }
}           

1.3.3 switch特點

/*
    switch後面小括号中隻能放 byte short int char String enum(枚舉)
 */
public class Demo06Switch特點 {
    public static void main(String[] args) {
        int a = 10;
        switch (a){}
        char b = '0';
        switch (b){}
        short c = 20;
        switch (c){}
//        long d = 10L;
//        switch (d){}
        String s ="";
        switch (s){}
        WEEK week = WEEK.MONDAY;
        switch (week){}
   }
}
enum WEEK{
    MONDAY,SUNDAY
}
enum MONTH{

}           

1.4循環結構for循環

1.4.1 for循環

/*
    循環結構:
        有明确的的開始和結束标志,循環往複執行一段代碼
       分類:
        1.for循環
        2.while循環
        3.do-while循環
     for循環格式:
        for(初始化語句;條件判斷語句;條件控制語句){
            循環體語句;
        }
        執行流程:
            1.初始化語句
            2.執行條件判斷語句 ==> true 執行循環體語句 ==> 條件控制語句 ==>  2.執行條件判斷語句
                            ==> false 結束for循環
 */
public class Demo01For循環 {
    public static void main(String[] args) {
        //列印1--5
        for (int i=1;i<=5;i++){
            System.out.println(i);
        }
        System.out.println("---------------");
        //列印5--1
        for (int i=5;i>=1;i--){
            System.out.println(i);
        }
    }
}           

1.4.2 For循環求和

public class Demo02For循環求和 {
    public static void main(String[] args) {
        //求1--100之間所有數字和
        int sum = 0;
        //周遊1--100之間所有的數字
        for (int i = 1; i <= 100; i++) {
            sum += i;
        }
        System.out.println("sum = "+sum);
    }
}           

1.4.3 For循環求奇數和

//求1--100之間所有奇數和
public class Demo03For循環求奇數和 {
    public static void main(String[] args) {
        int sum = 0;
        //循環周遊擷取1--100之間所有資料和,資料必須是奇數
        /*for (int i = 1; i <= 100; i++) {
            //判斷是否是奇數,如果是累加求和
            if (i%2!=0){
                sum+=i;//sum = sum+i;
            }
        }*/
        for (int i = 1; i < 100; i+=2) {
            sum+=i;
        }
        System.out.println("1--100之間所有奇數和:"+sum);
    }
}           

1.4.4For循環求偶數和

//求1--100之間所有偶數和
public class Demo04For循環求偶數和 {
    public static void main(String[] args) {
        //定義求和變量
        int sum = 0;
        //周遊1--100之間所有數字,篩選出偶數,累加
        /*for (int i = 1; i <= 100; i++) {
            if (i%2==0) //if語句後面如果隻有一行代碼,大括号可以省略不寫
                sum+=i;
        }*/
        for (int i = 0; i <= 100; i+=2) {
            sum+=i;
        }
        System.out.println("1--100之間所有偶數和:"+sum);
    }
}           

1.4.5For循環統計

/*
    統計1--100之間能被2整數的數字的個數,能被3整除的數字的個數
 */
public class Demo05For循環統計 {
    public static void main(String[] args) {
        //定義兩個統計變量
        int count1 = 0; //統計能被3整除個數
        int count2 = 0; //統計能被2整除個數
        //使用for循環周遊1--100之間數字
        for (int i = 1; i <= 100; i++) {
            //篩選數字
            if (i % 3 == 0)
                count1++;

            if (i % 2 == 0)
                count2++;

        }
        System.out.println("count1 = " + count1);
        System.out.println("count2 = " + count2);

    }
}           

1.4.6 統計水仙花數

/*
    統計水仙花數個數,并列印,兩個一行
    分析:
        水仙花數:是3位數 100-999 ;各個位上數字的立方和等于數字本身
        使用循環周遊100--999之間所有數字
        求每一個數字各個位上數字,然後求立方和
        判斷立方和 和 數字本身是否相等
        如果相等就是水仙花數
     步驟:
        1.使用for循環擷取每一個三位數
        2.求三位數各個位上的數字
        3.求各個位上數字的立方和
        4.判斷立方和是否和數字本身相等
 */
public class Demo06統計水仙花數 {
    public static void main(String[] args) {
        //定義統計變量
        int count = 0;
        //1.使用for循環擷取每一個三位數
        for (int i = 100; i <= 999; i++) {
            //2.求三位數各個位上的數字
            int ge = i % 10;
            int shi = i / 10 % 10;
            int bai = i / 100 % 10;
            //3.求各個位上數字的立方和
            double sum = Math.pow(ge, 3) + Math.pow(shi, 3) + Math.pow(bai, 3);
            //4.判斷立方和是否和數字本身相等
            if (sum == i) {
                //統計水仙花數的個數
                count++;
                System.out.print(i + " ");
                if (count % 2 == 0) {
                    System.out.println();
                }

            }
        }
    }
}
           

1.4.7 逢七必過

/*
    1--100 逢七必過:數字隻要包含7就喊過 是7的倍數 或者數字有7 就要喊過

    continue:關鍵字的意思是跳過本次繼續下一次
 */
public class Demo07逢七必過 {
    public static void main(String[] args) {
        /*for (int i = 0; i < 10; i++) {
            if (i==7){
                System.out.println("過");
                continue;
            }
            System.out.println(i); // 0 1 3 4
        }*/
        int count =0 ;
        //周遊1--100之間的數字
        for (int i = 1; i < 100; i++) {
            //判斷 是否包含7 :是7的倍數 或者個位上是7 或者十位是7
            if (i%7==0 || i%10 == 7 || i/10%10 == 7){

                System.out.println("過");

                continue;
            }
            System.out.println(i);

        }
    }
}
           

1.4.8列印星号

/*
    @@@@@
    @@@@@
    @@@@@
    @@@@@

 */
public class Demo08列印星号 {
    public static void main(String[] args) {
        for (int j = 0; j < 4; j++) { //外層循環控制行數
            for (int i = 0; i < 5; i++) { //内層循環控制列數
                System.out.print("@");
            }
            System.out.println();
        }
    }
}           

1.4.9 九九乘法表

public class Demo09九九乘法表 {
    public static void main(String[] args) {
        for (int i = 1; i <= 9; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(j+"*"+i+"="+i*j +"\t");
            }
            System.out.println();
        }
    }
}           

1.5while循環

1.5.1 while循環

/*
    while循環
        格式:
            初始化語句
            while(條件判斷語句){
                循環體語句;
                條件控制語句
            }
         執行流程:
            1.初始化語句
            2.條件判斷語句 ==》true 循環體語句 ==》條件控制語句 ==》 2.條件判斷語句
                        ==》false 結束循環
 */
public class Demo10while循環 {
    public static void main(String[] args) {
        //列印1--5
        for (int i=1;i<=5;i++){
            System.out.println(i);
        }
        System.out.println("---------------");
        int i= 1;
        while (i<=5){
            System.out.println(i);
            i++;
        }
        System.out.println("---------------");
        //列印5--1
        /*for (int i=5;i>=1;i--){
            System.out.println(i);
        }*/
        int j = 5;
        while (j>=1){
            System.out.println(j);
            j--;
        }

    }
}
           

1.5.2 珠穆朗瑪峰

/*
    需求:世界最高山峰是珠穆朗瑪峰(8844.43米=8844430毫米),假如我有一張足夠大的紙,它的厚度是0.1毫米。
請問,我折疊多少次,可以折成珠穆朗瑪峰的高度?

步驟:
    1.先定義紙張厚度
    2.定義珠峰高度
    3.運算:
        隻要紙張的厚度小于珠峰高度,就繼續折疊
        紙張每次折疊都要是上一次2倍
        每折疊一次,就要記錄一次
 */
public class Demo11珠穆朗瑪峰 {
    public static void main(String[] args) {
        //1.先定義紙張厚度
        double paper = 0.1;
        //2.定義珠峰高度
        double zf = 8844430;
        //3.定義計數器
        int count = 0;
        //4.統計折疊次數
        while (paper<zf){
            paper *= 2;
            count++;
        }
        System.out.println("折疊:"+count+"次");
    }
}
           

1.6 do-while循環

1.6.1 do-while循環

/*
    do-while循環
        格式:
            初始化語句
            do{
                循環體語句;
                條件控制語句
            }while(條件判斷語句);
        執行流程:
            1.初始化語句
            2.循環體語句
            3.條件控制語句
            4.條件判斷語句 ==> true 循環體語句 ==> 條件控制語句 ==> 4.條件判斷語句;
                        ==> false 結束循環
 */
public class Demo12DoWhile {
    public static void main(String[] args) {
        //列印1--5
        for (int i=1;i<=5;i++){
            System.out.println(i);
        }
        System.out.println("---------------");
        int i= 1;
        while (i<=5){
            System.out.println(i);
            i++;
        }
        System.out.println("---------------");
        int j = 1;
        do {
            System.out.println(j);
            j++;
        }while (j<=5);
    }
}
           

1.7 三種循環差別

/*
    1.for循環的初始化語句在循環内部,出來循環就不能使用
    2.while和do-while初始化語句在循環外面,循環結束後,可以繼續使用
    3.for循環和while循環 先判斷後執行 ,do-while先執行後判斷
 */
public class Demo13三種循環差別 {
    public static void main(String[] args) {
        //列印1--5
        for (int i=1;i<=5;i++){
            System.out.println(i);
        }

        System.out.println("---------------");
        int i= 1;
        while (i<=5){
            System.out.println(i);
            i++;
        }
        System.out.println(i);
        System.out.println("---------------");
        int j = 1;
        do {
            System.out.println(j);
            j++;
        }while (j<=5);
    }
}
           

1.8 死循環

/*
    死循環:
        for(;;){}
        while(true){}  //推薦使用
        do{}while(true);
 */
public class Demo14死循環 {
    public static void main(String[] args) {
        /*for(;;){
            System.out.println("不要停");
        }*/
        /*while (true){
            System.out.println("停不下來了");
        }*/
        do {
            System.out.println("受不了啦......");
        }while (true);
    }
}           

2.随機數

2.1 随機數入門

import java.util.Random;

/*
    Random類 用來生成随機數的
    步驟:
        1.導包
        import java.util.Random
        2.建立對象
        Random r = new Random();
        3.生成随機數
        int num = r.nextInt(); //生成int取值範圍内的随機數
        int num = r.nextInt(bound); //生成 0--bound之間随機數 包含0不包含bound  bound必須是int類型的正數
 */
public class Demo01随機數入門 {
    public static void main(String[] args) {
        Random random = new Random();

        /*for (int i=0;i<1000;i++) {
            ////生成int取值範圍内的随機數
            int num = random.nextInt();
            System.out.println(num);
        }*/

        //生成 0--bound之間随機數 包含0不包含bound
        int i = random.nextInt(10);
        System.out.println(i);
        //bound必須是int類型的正數
//        int num = random.nextInt(-10); //IllegalArgumentException  bound must be positive
//        System.out.println(num);

        //生成 20 -- 90之間随機數
        int num  = random.nextInt(71)+20;//0--89
        //生成33 -- 99之間的随機數
        int num2 = random.nextInt(67)+33;

        /*
            生成區間内的随機數: [最小值 ,最大值]
                int num = random.nextInt(最大值-最小值+1)+最小值;
         */
        //生成 -10 -- 10之間的随機數
        int num3 = random.nextInt(21)-10;
    }
}           

2.2猜數字

import java.util.Random;
import java.util.Scanner;

/*
    需求:程式自動生成一個1-100之間的數字,使用程式實作猜出這個數字是多少?
    分析:
        1.随機數 Random
        2.猜數字 Scanner
        3.不知道猜多少次 死循環 while(true)
     步驟:
        1.生成随機數 1--100
        2.猜數字
            2.1 鍵盤錄入數字
            2.2 判斷如果大了 提示猜大了 如果小了 提示猜小了 如果相等 恭喜你猜對了
 */
public class Demo02猜數字 {
    public static void main(String[] args) {
        //1.生成随機數 1--100
        Random random = new Random();
        int guessNum = random.nextInt(100)+1;
        //System.out.println(guessNum);
        //2.猜數字
        //2.1 鍵盤錄入數字
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("請輸入一個1--100之間的數字:");
            int num = sc.nextInt();
            if (num>guessNum){
                System.out.println("您猜大了!");
            }else if (num<guessNum){
                System.out.println("您猜小了!");
            }else if (num == guessNum){
                System.out.println("恭喜你,猜對了!晚上可以Happy了");break; //結束離break最近循環
            }
        }
    }
}
           

3 流程跳轉控制語句

/*
    流程跳轉控制語句:
        continue; 跳過本次 繼續下一次
        break; 在循環中 用來結束循環,但是結束目前循環或者說是離我最近循環
 */
public class Demo01 {
    public static void main(String[] args) {
        // continue; 跳過本次 繼續下一次
        for (int i = 0; i < 5; i++) {
            if (i==2){
                continue;
            }
            System.out.println(i);
        }
        System.out.println("-------");
        //break; 在循環中 用來結束循環,但是結束目前循環或者說是離我最近循環
        for (int i = 0; i < 5; i++) {
            if (i==2){
                break;
            }
            System.out.println(i);
        }
        //但是結束目前循環或者說是離我最近循環
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 5; j++) {
                if (j == 2){
                    break;
                }
                System.out.println("i = "+i +";   j = "+j);
            }
        }
        System.out.println("-----");
        //如果嵌套循環 想結束外層循環
        wc:for (int i = 0; i < 5; i++) {
            nc: for (int j = 0; j < 5; j++) {
                if (j == 2){
                    break wc;
                }
                System.out.println("i = "+i +";   j = "+j);
            }
        }
    }
}           

繼續閱讀