1. 程式人生 > 其它 >各種結構(順序、if、switch、while、do while)

各種結構(順序、if、switch、while、do while)

各種結構

1.順序結構

package com.kuang.struct;

public class ShunXuDemo {
    public static void main(String[] args) {
        System.out.println("hello01");
        System.out.println("hello02");
        System.out.println("hello03");
        System.out.println("hello04");
        System.out.println("hello05");
    }
}

執行結果為

2.if單選擇結構

語法

if(布林表示式){
//如果布林表示式為ture將執行語句
}
package com.kuang.struct;

import java.util.Scanner;

public class IfDemo01 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("請輸入內容:");
        String s = scanner.nextLine();
        //equal:判斷字串是否相等
        if (s.equals("Hello")) {
            System.out.println(s);
        }
        System.out.println("End");
        scanner.close();
    }
}

執行結果為

3.if雙選擇結構

語法結構

if(布林值表示式){
//如果布林表示式的值為true
}else
//如果布林表示式的值為false
}
package com.kuang.struct;

import java.util.Scanner;

public class IfDemo02 {
    public static void main(String[] args) {
        //如果考試分數大於60分就是及格,小於60分就是不及格。
        Scanner scanner = new Scanner(System.in);
        System.out.println("請輸入成績:");
        int score = scanner.nextInt();

        if (score > 60) {
            System.out.println("及格");
        } else {
            System.out.println("不及格");
        }
        scanner.close();
    }
}

執行結果

4.if多選擇結構

語法結構

if(布林表示式1){
//如果布林表示式1的值為true執行程式碼
}else if(布林表示式2){
//如果布林表示式2的值為true執行程式碼
}else if(布林表示式3){
//如果布林表示式3的值為true執行程式碼
}else{
//如果以上布林表示式都不為true執行程式碼
}
package com.kuang.struct;

import java.util.Scanner;

public class IfDemo03 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("請輸入成績:");
        int score = scanner.nextInt();
        if (score == 100) {
            System.out.println("恭喜滿分");
        } else if (score < 100 && score >= 90) {
            System.out.println("A級");
        } else if (score < 90 && score >= 80) {
            System.out.println("B級");
        } else if (score < 80 && score >= 70) {
            System.out.println("C級");
        } else if (score < 70 && score >= 60) {
            System.out.println("D級");
        } else if (score < 60 && score >= 0) {
            System.out.println("不及格");
        } else {
            System.out.println("成績不合法");
        }
        scanner.close();
    }
}

執行結果

說明:

if語句至多有1個else語句。else語句在所有的else if之後

if語句可以有若干個else if語句,它們必須在else語句之前

一旦其中一個else if語句檢測為ture,其他的else if以及else語句都將跳過執行

5.巢狀的if結構

語法結構

if(布林表示式1){
//如果布林表示式1的值為true就執行程式碼
  if(布林表示式2){
//如果布林表示式2的值為true就執行程式碼
  }
}

思考:我們需要尋找一個數,在1-100之間?

6.switch多選擇結構

語法結構

switch(expression){
   case value:
     //語句
     break;//可選
   case value:
    //語句
   break;//可選
  //你可以任意選擇數量的case語句
   default://可選
    //語句
}

注:break很重要,必須要寫,不然會引起case穿透

package com.kuang.struct;

public class SwitchDemo02 {
    public static void main(String[] args) {
        //JDK7的新特性,表達結果可以是字串
        String name = "秦疆";

        switch (name) {
            case "秦疆":
                System.out.println("秦疆");
                break;
            case "狂神":
                System.out.println("狂神");
                break;
            default:
                System.out.println("弄啥嘞");
        }
    }
}

執行結果

拓展

反編譯器(IDEA)

首先開啟檔案中專案結構,複製class路徑,去此電腦開啟路徑並找到想要反編譯的class

返回IDEA,右擊struct,找到show in project,找到該java檔案。

把剛才此電腦的class檔案拖到現在的java檔案裡,專案目錄便可以看到,雙擊即可看到反編譯。

初學一定要養成看原始碼的好習慣

6.while迴圈

語法結構

while(布林表示式){
//迴圈內容
}

特點:

只要布林表示式為ture,迴圈就會一直執行下去

我們大多數情況是會讓迴圈停止下來的,我們需要讓一個表示式失效的方式來結束迴圈

少部分迴圈需要程式一直執行,比如伺服器的請求響應監聽等

迴圈條件一直為true就會造成死迴圈,我們正常的業務中應當避免這種,會影響程式效能或者造成程式卡死崩潰

package com.kuang.struct;

public class WhileDemo01 {
    public static void main(String[] args) {
        //輸入1~100
        int i = 0;
        while (i < 100) {
            i++;
            System.out.println(i);
        }
    }
}

執行結果

思考題:計算1+2+3+4+....+100=?

package com.kuang.struct;

public class WhileDemo03 {
    public static void main(String[] args) {
        //計算1+2+3+4+....+100=?
        int i = 0;
        int sum = 0;
        while (i <= 100) {
            sum = sum + i;
            i++;
        }
        System.out.println(sum);
    }
}

執行結果為

7.dowhile迴圈

語法結構

do{
//程式碼語句
}whlie(布林表示式);

特點:先執行後判斷

即使不滿足條件,至少要也能執行一次

package com.kuang.struct;

public class DoWhileDmo01 {
    public static void main(String[] args) {
        int i = 0;
        while (i < 0) {
            System.out.println(i);
        }
        System.out.println("==========================");
        do {
            System.out.println(i);
            i++;
        } while (i < 0);
    }
}

執行結果

這裡可以看到while和dowhile的區別,while先判斷,後執行,這裡i=0,不小於0,所以程式跳過它執行下一步