Java流程式控制制

來源:互聯網
上載者:User

標籤:整數   put   變數   process   pos   表達   條件   判斷   次數   

     Java流程式控制制包括順序控制、條件控制和迴圈控制

  • 順序控制,就是從頭到尾依次執行每條語句操作
  • 條件控制,基於條件選擇執行語句,比方說,如果條件成立,則執行操作A,或者如果條件成立,則執行操作A,反之則執行操作B
  • 迴圈控制,又稱為迴路控制,根據迴圈初始條件和終結要求,執行迴圈體內的操作

    順序結構只能順序執行,不能進行判斷和選擇,因此需要分支結構

   分支結構:

  • if語句
  • if....else....
  • if.....else if....else if......
  • switch語句

    注意:if...else...條件的執行語句塊{ }如果只有一條語句的話,那麼這一對{ }可以省略;

            switch語句一旦滿足case條件,就執行case的相應語句。如果沒有break或者已經到結尾的話,會繼續執行其下的case語句! case後的變數類型可以為byte,short,char,int,枚舉,String且必須是常量或者值為常量的運算式

            do....while和while的區別是do...while迴圈至少會執行一次

            迴圈次數不確定的時候用while,迴圈次數確定的時候可以隨機使用

            一般情況下,在無限迴圈內部要有程式終止的語句,使用break實現。若沒有,那就是死迴圈

   迴圈結構:

  • while迴圈
  • do…while迴圈
  • for迴圈
  • foreach迴圈

     程式碼範例:

package processcontrol;import java.util.Scanner;public class ProcessControl {    public static void main(String[] args) {        // ifStudy();        // switchStudey();        //forStudy();        //whileStudy();        foreachStudy();    }    public static void ifStudy() {        Scanner input = new Scanner(System.in);        System.out.println("請輸入小名的期末成績:");        // 這裡預設輸入的是整數,不做詳細判斷        int score = input.nextInt();        if (score > 100 || score < 0) {            System.out.println("獎勵一輛BMW!");        } else {            if (score == 100) {                System.out.println("您輸入的數值有誤!");            } else if (score > 80 && score <= 99) {                System.out.println("獎勵一個台iphone5s!");            } else if (score >= 60 && score <= 80) {                System.out.println("獎勵一本參考書!");            } else {                System.out.println("沒及格,還想要獎勵!");            }        }    }    public static void switchStudey() {        Scanner input = new Scanner(System.in);        System.out.println("請輸入小名的期末成績:");        // 這裡預設輸入的是整數,不做詳細判斷        int score = input.nextInt();        switch (score / 60) {        case 1:            System.out.println("及格");            break;        case 0:            System.out.println("不及格");            break;        }        /*         * switch (score / 10) { case 10: case 9: case 8: case 7: case 6:         * System.out.println("及格"); break; case 5: case 4: case 3: case 2: case         * 1: case 0: System.out.println("不及格"); break; }         */    }    public static void forStudy() {        /*         * 編寫程式FooBizBaz.java,從1迴圈到150並在每行列印一個值,         * 另外在每個3的倍數行上列印出“foo”,在每個5的倍數行上列印“biz”, 在每個7的倍數行上列印輸出“baz”         */        for (int i = 1; i <= 150; i++) {            System.out.print(i);            if (i % 3 == 0) {                System.out.print(" foo");            }            if (i % 5 == 0) {                System.out.print(" biz");            }            if (i % 7 == 0) {                System.out.print(" baz");            }            System.out.println();        }    }    public static void whileStudy() {        // 輸出100以內的偶數        /*        int i = 1;        while (i <= 100) {            if (i % 2 == 0) {                System.out.println(i);            }            i++;        }        */        int i=1;        do{            if(i%2==0){                System.out.println(i);            }            i++;        }while(i<=100);    }    public static void foreachStudy() {        int[] arr={2,3,4,5,6};        for(int i:arr){            System.out.println(i);        }    }}

 

Java流程式控制制

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.