Java基礎(三),java基礎

來源:互聯網
上載者:User

Java基礎(三),java基礎
一、選擇結構,條件判斷1、if 語句

  一個 if 語句包含一個布林運算式和一條或多條語句。如果布林運算式的值為 true,則執行 if 語句中的代碼塊,否則執行 if 語句塊後面的代碼。

 1 import static java.lang.Math.round; 2  3 public class Conditional { 4     /** 5      * 簡單if語句 6      */ 7     @Test 8     public void testIf() { 9         double random = Math.random();10         int num = (int) round(random * 10);  //建立一個隨機整數11         System.out.println(num);12         if (num < 3) {13             System.out.println("num小於3");14         } else {15             System.out.println("num大於等於3");16         }17     }18 19     /**20      * 簡單if, else if,else語句21      */22     @Test23     public void testIf2() {24         double random = Math.random();25         int num = (int) round(random * 10);  //建立一個隨機整數26         System.out.println(num);27         if (num < 3) {28             System.out.println("num小於3");29         } else if(num < 6) {30             System.out.println("num小於6");31         }else {32             System.out.println("num大於等於6");33         }34     }35 36     /**37      * 嵌套if語句38      */39     @Test40     public void testIf3() {41         double random = Math.random();42         int num = (int) round(random * 10);  //建立一個隨機整數43         System.out.println(num);44         if (num < 3) {45             System.out.println("num小於3");46         } else if(num < 6) {47             System.out.println("num小於6");48         }else {49             if(num == 6){50                 System.out.println("num等於6");51             }52             System.out.println("num大於6");53         }54     }55 }
2、 switch 語句

  switch 語句判斷一個變數與一系列值中某個值是否相等,每個值稱為一個分支。

文法:

switch(變數或者一個運算式){case 變數的可能值1: 功能語句;
break; case 變數的可能值2: 功能語句;
break;case 變數的可能值3: 功能語句;
break;........default:功能語句;}

switch 語句規則:

  • switch 語句中的變數類型可以是: byte、short、int 或者 char。從 Java SE 7 開始,switch 支援字串類型了

  • switch 語句可以擁有多個 case 語句。每個 case 後面跟一個要比較的值和冒號。

  • case 後面值的資料類型必須和變數的資料類型一致,而且只能是常量或者字面常量。

  • 當變數的值與 case 語句的值相等時,那麼 case 語句之後的語句開始執行,直到 break 語句出現才會跳出 switch 語句。

  • 如果沒有 break 語句出現,程式會繼續執行下一條 case 語句,直到出現 break 語句。

  • 按照case的值的大小順序排列,default放最後,default 在沒有 case 語句的值和變數值相等的時候執行。default 分支不需要 break 語句。

 1 import static java.lang.Math.round; 2  3 public class Conditional1 { 4     /** 5      * switch語句 6      */ 7     @Test 8     public void testSwitch(){ 9         double random = Math.random();10         int num = (int) round(random * 10);11         System.out.println(num);12         switch(num){13             case 0 :14                 System.out.println("num為0");15                 break;16             case 1 :17                 System.out.println("num為1");18                 break;19             case 2 :20                 System.out.println("num為2");21                 break;22             case 3 :23                 System.out.println("num為3");24                 break;25             case 4 :26                 System.out.println("num為4");27                 break;28             case 5 :29                 System.out.println("num為5");30                 break;31             default :32                 System.out.println("num大於5");33         }34     }35 }
二、迴圈結構1、while迴圈

  先判斷條件,再執行語句

2、do-while迴圈

  先執行一次,再判斷條件

 1 public class Circulation { 2     /** 3      * while迴圈語句 4      * 先判斷,再執行 5      */ 6     @Test 7     public void testWhile() { 8         int num = 5; 9         while (num > 0) {10             System.out.println(num);11             num -= 1;12         }13     }14 15     /**16      * do-while迴圈17      * 先do執行一次,再判斷18      */19     @Test20     public void testDoWhile() {21         int num = 5;22         do {23             System.out.println(num);24             num -= 1;25         } while (num > 0);26     }27 }
3、for迴圈

  for迴圈執行的次數是在執行前就確定的。

文法:

for(初始化語句A ; 條件判斷B; 迴圈後功能語句C){    //迴圈體D}

  執行一次迴圈後,更新迴圈控制變數,語句C的作用。然後再次檢測布林運算式。迴圈執行上面的過程。

 1 public class Circulation { 2     /** 3      * 簡單for迴圈 4      */ 5     @Test 6     public void testFor() { 7         int num = 10; 8         for (int i = 0; i < num; i++) { 9             System.out.println(i);10         }11     }12 }
4、foreach加強的for迴圈

  Java5 引入了一種主要用於數組的增強型 for 迴圈。

Java 增強 for 迴圈文法:

for(資料類型  值: 數組){   //代碼句子}
 1 public class Circulation1 { 2     /** 3      * foreach迴圈 4      */ 5     @Test 6     public void testForeach() { 7         int[] num = {10, 20, 30, 40, 50}; 8         for (int i : num) { 9             System.out.println(i);10         }11     }12 }
5、嵌套迴圈
 1 public class Circulation { 2     /** 3      * 嵌套迴圈實現冒泡排序 4      */ 5     @Test 6     public void test() { 7         int[] num = {12, 2, 7, 5, 14}; 8         int t = 0; 9         for (int i = 0; i < num.length-1; i++) {10             for (int j = 0; j < num.length-1; j++){11                 if (num[j] > num[j + 1]) {12                     t = num[j];13                     num[j] = num[j + 1];14                     num[j + 1] = t;15                 }16             }17         }18         System.out.println(Arrays.toString(num));19     }20 }
三、迴圈控制語句1、break

  break 表示終止當前這一層迴圈,即跳出當前迴圈。

2、continue

  continue 表示跳過本次迴圈,進入下一次迴圈。

3、return

  return 表示結束當前的方法。

注意:break、continue、return 後面都不能跟任何代碼,因為永遠都不會執行。

 

聯繫我們

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