java入門(4)--流程控制

選擇

程序若是隻是逐條地順序執行,那程序的行爲恐怕要簡單得多了,但也會失去大部分的強悍功能和精彩。java

正是「分支」打破了順序執行的呆板局面,給程序注入了真正的生命力。數組

java中的分支主要由 選擇循環語句提供,其語法基本與 c 語言相同。編碼

if...else... 恐怕是最爲咱們熟悉的了。它有多種表現形式。spa

if(條件) 語句;code

blog

if(條件){遊戲

語句1;作用域

語句2;
...it

}class

有的編碼規範上要求,即使是隻有一條語句,也要放在大括號中。

if(條件){

語句

}

else {

語句

}

if(條件){

語句

}

else if(條件2){

語句

}

else if(條件3){

語句

}

else{

語句

}

下面以多種風格求 a,b,c三個數中最大的數。

 1 public class A0407
 2 {
 3     public static void main(String[] args){
 4         int a = 15;
 5         int b = 12;
 6         int c = 29;
 7         
 8         int max = a;
 9         if(b>max) max = b;
10         if(c>max) max = c;
11         
12         System.out.println(max);
13     }
14 }

這種方式簡明易懂,只用 if 沒有 else

邏輯上先假設 a 是最大的,而後誰比它大,誰就成爲新任盟主。

 1 public class A0407
 2 {
 3     public static void main(String[] args){
 4         int a = 15;
 5         int b = 12;
 6         int c = 29;
 7                 
 8         if(a>=b && a>=c) System.out.println(a);
 9         if(b>=a && b>=c) System.out.println(b);
10         if(c>=a && c>=b) System.out.println(c);
11     }
12 }

這種方法也很容易理解。要注意是大於等於,不是大於哦,若是出現了相等數字 ....

其實這仍是有個 bug 的, 假如 3 個數字相等, 就會輸出 3 次的。

 1 public class A0407
 2 {
 3     public static void main(String[] args){
 4         int a = 15;
 5         int b = 12;
 6         int c = 29;
 7                 
 8         if(a>b){
 9             if(a>c)
10                 System.out.println(a);
11             else
12                 System.out.println(c);
13         }
14         else{
15             if(b>c)
16                 System.out.println(b);
17             else
18                 System.out.println(c);
19         }
20     }
21 }

這種方法有點點繞人,當待比較的數字更多的時候,簡直就災難了...

 1 public class A0407
 2 {
 3     public static void main(String[] args){
 4         int a = 15;
 5         int b = 12;
 6         int c = 29;
 7     
 8         if(a>=b && a>=c)
 9             System.out.println(a);
10         else if(b>=a && b>=c)
11             System.out.println(b);
12         else
13             System.out.println(c);
14     }
15 }

這樣也挺好吧? 不會擔憂數字重複的問題。

 若是有不少個else if 這樣的邏輯分支,多數狀況能夠用 switch 語句來代替,代碼更清楚一些。

以下的代碼把分數轉化爲評語。

 1 public class A0407
 2 {
 3     public static void main(String[] args){
 4         int score = 85;
 5         switch(score/10){
 6             case 9:
 7                 System.out.println("優秀");
 8                 break;
 9             case 8:
10                 System.out.println("良好");
11                 break;
12             case 7:
13                 System.out.println("中等");
14                 break;
15             case 6:
16                 System.out.println("及格");
17                 break;
18             default:
19                 System.out.println("不及格");
20         }
21     }
22 }

注意,switch 的括號內只能是可枚舉的類型,好比:int char 等。用 String 類型是不能夠的。

初學者還要注意,每一個 case 塊內,不要忘記了 break 語句,不然,程序在執行完該塊的內容後還會繼續執行下一塊中的代碼,這每每不是咱們所但願的。

循環

java 與 c 語言同樣,提供了 while for 等循環構造手段。

最簡單的循環莫過於「死循環」。能夠經過 while(true) 來實現。

下面的代碼打印出 10000 之內  5 的冪。

 1 public class A0407
 2 {
 3     public static void main(String[] args){
 4         int x = 1;
 5         while(true){
 6             if(x > 10000) break; 
 7             System.out.println(x);
 8             x *= 5;
 9         }
10     }
11 }

死循環通常老是要配合 break 語句,以便在適當的時機跳出循環外。

這個跳出條件也能夠轉化爲「不跳出條件」,直接寫在 while 的括號中。

 1 public class A0407
 2 {
 3     public static void main(String[] args){
 4         int x = 1;
 5         while(x<=10000){
 6             System.out.println(x);
 7             x *= 5;
 8         }
 9     }
10 }

觀察這裏的 x ,若是你寫過不少循環邏輯,就會發現規律:

這裏的變量老是執行:初始化控制跳出時機,改變自身 這3件事情。

for 語句就能夠把這 3 件事情 收納於一身。

上面的代碼邏輯用 for 表示起來就會更加簡潔:

1 public class A0407
2 {
3     public static void main(String[] args){
4         for(int x=1; x<=10000; x *= 5){
5             System.out.println(x);
6         }
7     }
8 }

但,通常狀況下,咱們更願意用 for 循環來表達已知次數的循環。

以下代碼求出一個數組中的最大值。

 1 public class A0407
 2 {
 3     public static void main(String[] args){
 4         int[] a = {3,15,22,7,16,4,7};
 5         int max = a[0];
 6         for(int i=1; i<a.length; i++){
 7             if(a[i] > max) max = a[i];
 8         }
 9         System.out.println(max);
10     }
11 }

咱們須要注意,for 循環中的控制變量 i 的做用域範圍僅僅限於 for 語句塊內。

也就是說,若是緊跟着還有一個 for 語句,它也能夠再定義 變量 i, 沒必要要換另外一個名字。

在循環中,總能夠經過 break 語句提早跳出來,也能夠經過 continue 語句越過本輪循環,跳到下一輪(但不跳出循環)。

下面的代碼模擬的是一個叫作「拍七」的小遊戲。

這個遊戲要求你們從1 到 100 輪流報數,但碰到含有 7 或 7 的倍數的數字要跳過去。

 1 public class A0407
 2 {
 3     public static void main(String[] args){
 4         for(int i=1; i<=100; i++){
 5             if(i%10==7) continue;
 6             if(i/10==7) continue;
 7             if(i%7==0) continue;
 8             System.out.println(i);        
 9         }
10     }
11 }

return 語句

除了選擇和循環, return 語句也是經常使用的流程控制手段。

下面的代碼,把判斷一個數字是否爲素數封裝爲一個方法來調用。

所謂素數就是除了 1 和它自己外,再沒有其它數字能夠整除它。

 1 public class A0407
 2 {
 3     static boolean sushu(int n){
 4         for(int i=2; i*i<=n; i++){
 5             if(n%i==0) return false;
 6         }
 7         return true;
 8     }
 9     
10     public static void main(String[] args){
11         System.out.println(sushu(3));
12         System.out.println(sushu(4));
13         System.out.println(sushu(5));
14         System.out.println(sushu(91));
15     }
16 }
相關文章
相關標籤/搜索