迴圈Loop

      在〈迴圈Loop〉中尚無留言

for 迴圈

for 迴圈的基本語法如下。

for (啟始值 ; 判斷式 ; 步進值){
}

第一次迴圈 : 先執行啟始值,再執行判斷式
第二次及以後 : 先執行步進值,再執行判斷式
所以如果迴圈跑了n次,則每個區段的執行次數如下

啟始值 : 1次
判斷式 : n次
步進值 : n-1次

    for(int i=1;i<=100;i++){
        total+=i;
    }
    System.out.println("1+2+3+...+100="+total);

上述之三區段並不是一定要填入才行,也可以變型如下

    int a=1, total=0;
    for(;;){
        if (a<=100)
            total+=a++;
        else
            break;
    }
    System.out.println("1+2+3+...+100="+total);

巢狀for迴圈

迴圈之內又有迴圈稱為巢狀迴圈,如下為九九乘法表。

    public static void main(String[] args){
        outloop:
        for(int i=1;i<=9;i++){
            innerloop:
            for (int j=1;j<=9;j++){
                System.out.printf("%2d ", i*j);
            }
            System.out.println();
        }
    }

執行結果 : 
 1  2  3  4  5  6  7  8  9 
 2  4  6  8 10 12 14 16 18 
 3  6  9 12 15 18 21 24 27 
 4  8 12 16 20 24 28 32 36 
 5 10 15 20 25 30 35 40 45 
 6 12 18 24 30 36 42 48 54 
 7 14 21 28 35 42 49 56 63 
 8 16 24 32 40 48 56 64 72 
 9 18 27 36 45 54 63 72 81

上述的 outloop: 及 innerloop: 只是替迴圈命名方便閱讀而以,實際上並沒什麼效益,一般都省略不寫

for之變型

在for() 的三區塊中可以使用 “,” 將多個程式碼寫入,如下

for (int i=0, int j=1 ; i<100, i<50 ; i++, j++){}

break與continue

不論是for、while、do-while,在裏面使用 break 即立刻跳離迴圈,遇到 continu 則馬上回到迴圈源頭進行判斷。

While 迴圈

while 基本語法如下

while (條件式){
}

while 迴圈如同 if 都需靠裏面的條件式判斷是否進入執行區塊,但 if 是在執行完後就跳出,而 while 迴圈則是在執行完後回到源頭再判斷一次是否要執行。

    public static void main(String[] args){
        int a=1, total=0;
        while (a<=100){
            total+=a++;
        }
        System.out.println("1+2+3+...+100="+total);
    }

在迴圈內亦可使用 break 讓迴圈中斷跳出,也可以使用 continue 直接回源頭判斷是否要再進入執行區域

do-while

    public static void main(String[] args){
        int a=1, total=0;
        do{
            total+=a++;
        }while(a<=100);
        System.out.println("1+2+3+...+100="+total);
    }

do-while 會先進入迴圈執行一次,再進行判斷,特別注意的是在 while() 之後要加上 “;” 結束符號

迴圈結構比較 :

while 執行 0 次以上
do/while 執行 1 次以上
for 執行一個預先就確定的次數

exam1

列印 
1 2 3 4 5 6 7 8 9

public static void main(String[] args) {
    for (int i=1;i<10;i++){
        System.out.printf("%d ", i);
    }
    System.out.println();
}

exam2

列印
* * * * *

public static void main(String[] args) {
    for (int i=0;i<5;i++){
        System.out.print("* ");
    }
    System.out.println();
}

exam3

列印
* * * * * 
* * * * * 
* * * * *

public static void main(String[] args) {
    for (int i=0;i<3;i++){
        for (int j=0;j<5;j++){
            System.out.print("* ");
        }
        System.out.println();
    }
}

exam4

列印
* 
* * 
* * * 
* * * * 
* * * * * 


public static void main(String[] args) {
    for (int i=1;i<=5;i++){
        for (int j=1;j<=i;j++){
            System.out.print("* ");
        }
        System.out.println();
    }
}

exam5

列印
* * * * * 
* * * * 
* * * 
* * 
*

public static void main(String[] args) {
    for (int i=5;i>=1;i--){
        for (int j=1;j<=i;j++){
            System.out.print("* ");
        }
        System.out.println();
    }
}
結果 : 

exam6

列印
* 
* * 
* * * 
* * * * 
* * * * *

public static void main(String[] args) {
    for (int i=1;i<=5;i++){
        for (int j=1;j<=5-i;j++){
            System.out.print(" ");
        }
        for (int j=1;j<=i;j++){
            System.out.print("* ");
        }
        System.out.println();
    }
 }

exam7

列印
> > > > >
< < < < <
> > > > > 
< < < < <
> > > > >

public static void main(String[] args) {
    for (int i=1;i<=5;i++){
        for (int j=1;j<=5;j++){
            if(i%2==1)System.out.print("> ");
            else System.out.print("< ");
        }
        System.out.println();
    }
}

exam8

列印
A A A A A
B B B B B
C C C C C
A A A A A
B B B B B

public static void main(String[] args) {
    for (int i=1;i<=5;i++){
        for (int j=1;j<=5;j++){
            if(i%3==1)System.out.print("A ");
            else if (i%3==2)System.out.print("B ");
            else System.out.print("C ");
        }
        System.out.println();
    }
}

Exam9

< < < < <
> > > > >
< < < < <
> > > > >
< < < < <

public static void main(String[] args) {
    for (int i=1;i<=5;i++){
        for (int j=1;j<=5;j++){
            if(i%2==0)
                System.out.print("> ");
            else
                System.out.print("< ");
        }
        System.out.println();
    }
}

Exam10

A A A A A
B B B B B
C C C C C
A A A A A
B B B B B

public static void main(String[] args) {
    for (int i=1;i<=5;i++){
        for (int j=1;j<=5;j++){
            if(i%3==1)
                System.out.print("A ");
            else if (i%3==2)
                System.out.print("B ");
            else
                System.out.print("C ");
        }
        System.out.println();
    }
}

Exam11

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
a b c d e f g h i j k l m n o p q r s t u v w x y z

public static void main(String[] args) {
    int start=65;
    for (int i=start;i<start+26;i++){
        System.out.printf("%c ", i);
    }
    System.out.println();
    start=97;
    for (int i=start;i<start+26;i++){
        System.out.printf("%c ", i);
    }
    System.out.println();
}

終極密碼

public class Password {
    public static void main(String[] args) {
        int pwd=(int)(Math.random()*98)+1;
        int min=1, max=100;
        int g=0;
        Scanner in=new Scanner(System.in);
        while(true){
            System.out.printf("請輸入密碼, 介於 %d ~ %d 間 : ", min, max);
            g=in.nextInt();
            if(g==pwd){
                System.out.println("Bingo");
                break;
            }
            else if (g<pwd)min=g;
            else max=g;
        }
    }
}
結果 :
請輸入密碼, 介於 1 ~ 100 間 : 50
請輸入密碼, 介於 1 ~ 50 間 : 25
請輸入密碼, 介於 1 ~ 25 間 : 12
請輸入密碼, 介於 12 ~ 25 間 : 20
請輸入密碼, 介於 20 ~ 25 間 : 22
請輸入密碼, 介於 20 ~ 22 間 : 21
Bingo

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *