Java中的"goto"實現

此文轉自:http://lavasoft.blog.51cto.com/62575/178997
Java中的"goto"實現
 
Java語言中goto是保留關鍵字,沒有goto語句,也沒有任何使用goto關鍵字的地方。
 
Java中也可在特定情況下,通過特定的手段,來實現goto的功能。顯然Java不願意開發者隨意跳轉程序。下面解釋兩個特定:
特定情況:只有在循環體內,比如for、while語句(含do...while語句)中。
特定手段:語句標籤和循環控制關鍵字break、continue,語法格式是:break/continue 語句標籤。
 
一、break、continue和語句標籤
 
1、語句標籤
語句標籤的語法是:標籤名:
語句標籤可以定義在方法體內的最後一條語句之前即可。但是語句標籤實際使用的機會是與break和continue結合使用的,而break和continue是和循環語句結合使用的,因此實際上語句標籤的使用也是和循環緊密結合的。
 
語句標籤在被使用的情況,只能定義在循環迭代語句之前,否則編譯出錯!
 
因此,有意義、可使用的標籤含義是:指定循環語句的標識!
 
2、break、continue語句單獨使用
單獨使用情況下:break語句作用是結束當前的循環迭代體,進而執行剩餘的語句。
continue語句的作用是結束本次迭代過程,繼續執行下一輪迭代。
 
3、break、continue語句結合語句標籤的使用
爲什麼需要語句標籤呢?
原因是因爲程序可能有循環的嵌套,當多層循環嵌套時候,有時候需要一次跳出多級循環,這種情況下就需要結合語句標籤才能實現此功能了。
 
帶標籤使用情況下:break中斷並跳出標籤所指定循環,continue跳轉到標籤指定的循環處,並繼續執行該標籤所指定的循環。
 
爲了說明情況,看看下面的例子:
import java.util.Random;

/**
* 語句標籤測試
*
* @author leizhimin 2009-7-16 11:43:08
*/

public class TestLable {
        public static void main(String[] args) {
                outer:
                for (int i = 0; i < 10; i++) {
                        System.out.println("/nouter_loop:" + i);
                        inner:
                        for (int k = 0; i < 10; k++) {
                                System.out.print(k + " ");
                                int x = new Random().nextInt(10);
                                if (x > 7) {
                                        System.out.print(" >>x == " + x + ",結束inner循環,繼續迭代執行outer循環了!");
                                        continue outer;
                                }
                                if (x == 1) {
                                        System.out.print(" >>x == 1,跳出並結束整個outer和inner循環!");
                                        break outer;
                                }
                        }
                }
                System.out.println("------>>>所有循環執行完畢!");
        }
}
 
執行結果:

outer_loop:0
0 1 2 3 4 5 6 7 8 9    >>x == 8,結束inner循環,繼續迭代執行outer循環了!
outer_loop:1
0 1 2 3 4 5    >>x == 9,結束inner循環,繼續迭代執行outer循環了!
outer_loop:2
0 1 2 3 4 5 6 7 8 9    >>x == 8,結束inner循環,繼續迭代執行outer循環了!
outer_loop:3
0 1 2 3 4    >>x == 9,結束inner循環,繼續迭代執行outer循環了!
outer_loop:4
0 1 2 3 4 5 6 7 8 9 10    >>x == 8,結束inner循環,繼續迭代執行outer循環了!
outer_loop:5
0    >>x == 1,跳出並結束整個outer和inner循環!------>>>所有循環執行完畢!

Process finished with exit code 0
 
這個執行結果是隨機的。
 
下面給個圖看看:
 
二、switch語句
 
switch語句是一個條件選擇語句,這個語句有“goto”的味道,但是限制也很多,因此,實際中使用較少。
 
switch語句的結構如下:
switch(intvar){
        case intval: 語句代碼;break;
        case intval: 語句代碼;break;
        case intval: 語句代碼;break;
        case intval: 語句代碼;break;
        default:
                 語句代碼;
}
 
switch(intval){...}語句中,小括號中intvar是一個整數條件因子變量,這個變量只能爲:byte、char、short、int和enum(枚舉類型)幾種類型,本質上都是整形數字。intval是匹配的條件因子值,當匹配時,執行其下的語句。其中所有的break語句都是可選的。當執行了break語句後,就跳出整個switch語句,否則,還會繼續往下匹配別的條件。當intvar不能匹配所有的給定條件值時候,就執行default語句,如果沒有default語句,則跳出switch語句。
 
switch語句的條件因子變量只能作爲整型數字或者字符型、枚舉類型,這個限制太嚴格了,使得switch語句的實際用途不是很大。
 
下面是一個漢語金額數字轉換程序:
/**
* 漢語金額數字轉換程序
*
* @author leizhimin 2009-7-16 13:28:05
*/

public class TestSwitch {

        /**
         * 數字轉換爲漢語金額數字
         *
         * @param num 數字
         * @return 漢語金額數字
         */

        public static String genCnNum(Long num) {
                StringBuffer sb = new StringBuffer();
                String snum = String.valueOf(num);
                for (char c : snum.toCharArray()) {
                        sb.append(num2Cn(c));
                }
                return sb.toString();
        }

        /**
         * 字符數字轉換爲漢語金額數字
         *
         * @param c 字符數字
         * @return 漢語金額數字
         */

        private static String num2Cn(char c) {
                String res = null;
                switch (c) {
                        case '0':
                                res = "零";
                                break;
                        case '1':
                                res = "壹";
                                break;
                        case '2':
                                res = "貮";
                                break;
                        case '3':
                                res = "叄";
                                break;
                        case '4':
                                res = "肆";
                                break;
                        case '5':
                                res = "伍";
                                break;
                        case '6':
                                res = "陸";
                                break;
                        case '7':
                                res = "柒";
                                break;
                        case '8':
                                res = "捌";
                                break;
                        case '9':
                                res = "玖";
                                break;
                        default:
                                System.out.println("您的輸入有誤,請重試!");
                }
                return res;
        }

        public static void main(String[] args) {
                System.out.println(genCnNum(4523586022L));
        }
}
 
運行結果:
肆伍貮叄伍捌陸零貮貮

Process finished with exit code 0

 

 

 

發佈了60 篇原創文章 · 獲贊 8 · 訪問量 14萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章