選擇結構if語句和switch語句的區別,結構if語句switch

來源:互聯網
上載者:User

選擇結構if語句和switch語句的區別,結構if語句switch

------- android培訓、java培訓、期待與您交流! ----------

 

1.選擇結構if語句格式及其使用 

A:if語句的格式:

  if(比較運算式1) {     語句體1;  }else if(比較運算式2) {     語句體2;  }else if(比較運算式3) {     語句體3;  }     ...  else {    語句體n+1;  }B:執行流程:  首先計算比較運算式1看其傳回值是true還是false,  如果是true,就執行語句體1,if語句結束。  如果是false,接著計算比較運算式2看其傳回值是true還是false,  如果是true,就執行語句體2,if語句結束。  如果是false,接著計算比較運算式3看其傳回值是true還是false,  如果都是false,就執行語句體n+1。C:注意事項:最後一個else可以省略,但是建議不要省略,可以對範圍外的錯誤值提示 eg:
 1 import java.util.Scanner; 2 class Demo_If { 3     public static void main(String[] args) { 4         Scanner sc = new Scanner(System.in);   //鍵盤輸入, 5         while(true) { 6         System.out.println("請您輸入成績");    //提示輸入 7         int a = sc.nextInt();                    //鍵盤輸入用int類型接收 8         if (a>100|a<0) {                        //死迴圈,方便測試 9             System.out.println("您輸入的成績有誤");10         }else if (a>=90&a<=100) {11             System.out.println("A等");12         }else if (a>=80&a<90) {13             System.out.println("B等");14         }else if (a>=70&a<80) {15             System.out.println("C等");16         }else if (a>=60&a<70) {17             System.out.println("D等");18         }else if (a<60) {19             System.out.println("E等");20         }else {21             System.out.println("您輸入的成績有誤");22         }23         }24 }25 }

 

2.選擇結構switch語句格式及其使用A:switch格式:  switch(運算式) {        case 值1:      語句體1;      break;       case 值2:      語句體2;      break;          …       default:      語句體n+1;      break;        } B:switch語句的格式解釋  (基礎資料型別 (Elementary Data Type),只要能提升為int的都可以,引用資料類型中的枚舉(JDK1.5)和String(JDK1.7))C:執行流程   先計算運算式的值  然後和case後面的匹配,如果有就執行對應的語句,否則執行default控制的語句eg
 1 import java.util.*; 2 class  Dome_If3{ 3     public static void main(String[] args) { 4         //System.out.println("Hello World!"); 5         Scanner sc =new Scanner(System.in);        //鍵盤錄入 6         while(true) {                                //死迴圈方便測試 7         System.out.println("請您    輸入要轉換的星期數");  //鍵盤錄入提示 8         in week = sc.nextInt(); 9         switch(week) {10             case 1:11                 System.out.println("星期1");12             break;13             case 2:14                 System.out.println("星期2");15             break;16             case 3:17                 System.out.println("星期3");18             break;19             case 4:20                 System.out.println("星期4");21             break;22             case 5:23                 System.out.println("星期5");24             break;25             case 6:26                 System.out.println("星期6");27             break;28             case 7:29                 System.out.println("星期天");30             break;31             default:32                 System.out.println("您輸入的數字有誤,請重新輸入");33         }34       }35     }36 }

 

3:總結switch語句和if語句的各自使用情境  switch建議判斷固定值的時候用  if建議判斷區間或範圍的時候用      *用switch能做的,用if都能做,單反過來則不行

------- Windows Phone 8手機開發、.Net培訓、期待與您交流! -------

聯繫我們

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