think in JAVA之Label,JAVA中的Goto__JAVA

來源:互聯網
上載者:User

最近想練練基礎功,於是拿出JAVA經典書think in java,邊學邊記一些自己的感受。

首先,今天要說的就是標籤Label,JAVA中保留了goto關鍵字,卻沒有goto語句。取而代之的是Label。曾幾何時如果早點知道這個,就好了。看了think in java,表示學習了。

package com.tudou.t1;/** * 需求:遇到一種情況,當內層迴圈有一個條件滿足時,退出整個迴圈 * @author lz * */public class LabelGoto {public static void main(String[] args) {test();//原始方法不能滿足需求test1();//原始做法test2();//使用Label的做法}private static void test() {for (int i = 0; i < 10; i++) {for (int j = 0; j < 20; j++) {if (j == 10) {System.out.print("中斷");break;//只能中斷內層迴圈}}System.out.print(i);}}private static void test1() {boolean flag = false;for (int i = 0; i < 10; i++) {for (int j = 0; j < 20; j++) {if (j == 10) {flag = true;//改變變數的值System.out.println("中斷");break;}}if (flag == true) {System.out.println("變數中斷外層迴圈:" + i);break;}}}private static void test2() {L: // 聲明一個書籤for (int i = 0; i < 10; i++) {for (int j = 0; j < 20; j++) {if (j == 10) {System.out.println("使用Label直接中斷");break L;}}System.out.print(i);}System.out.println("迴圈中斷。");}}

第一種方法解決不了問題,而第二種方法是我們在沒有接觸Lable時用的最常用手段。

而學習了Label後,發現。。。和SQL中一樣的道理。只是一直不知道JAVA中也有標籤。

聯繫我們

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