java switch的應用

來源:互聯網
上載者:User

標籤:ann   border   []   color   適用於   成績   bsp   order   void   

變數的值,與case後的那個值一樣的話,執行那個case後的語句,遇到break跳出switch結構。

如果case任何一個值都與變數的內容不一樣的執行default後的語句;

break;可省略,不推薦

 break   

  • 打斷
  • 打破
  • 跳出迴圈

    if, if..else...

    switch

    更適用於區間範圍的判斷:從...到...

    更實用與等值比較

    if,if...else...一定可以體會switc

    switch不一定能替換if,if...else...

    import java.util.Scanner;public class Dome1{    public static void main (String []args ){        Scanner input = new Scanner (System.in );        int day =0;        System.out.print("請輸入你的成績:");        day = input.nextInt();        if(day>100||day <0){            System.out.print("輸入成績錯誤");        }        else{            day=day/10;        switch(day){                case 10:System.out.print("A");break;                case 9:System.out.print("B");break;                case 8:System.out.print("C");break;                case 7:System.out.print("D");break;                case 6:System.out.print("E");break;                default:System.out.print("不及格");                        }        }                                    }}

     

     

     

    ... ...

    if(day == 1) System.out.println(“星期1”);

    else if(day == 2) System.out.println(“星期2”);

    else if(day == 3) System.out.println(“星期3”);

    else if(day == 4) System.out.println(“星期4”);

    else if(day == 5) System.out.println(“星期5”);

    else if(day == 6) System.out.println(“星期6”);

    else if(day == 7) System.out.println(“星期日”);

    else System.out.println(“輸入錯誤!”);

    ... ...

    ... ...

    switch(day){

    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("輸入錯誤!");

    }

    ... ...

     
       

 

java switch的應用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.