今天有人問我Java中有沒有goto?

來源:互聯網
上載者:User

        今天有人問我java中有沒有goto,一看他的問題就知道他是學C的,呵呵,以前我還真的沒太注意這個問題,因為我知道goto是java中的關鍵字,但是好像從來沒用過,他又問我,如果想跳出幾個迴圈體怎麼操作啊?

        這個問題嘛,我還是有辦法地……

  1. public class TestFor 
  2. {
  3.  public static void main(String[] args) 
  4.  {
  5.   TestFor test = new TestFor();
  6.   test.testLoopOne();
  7.   System.out.println();
  8.   System.out.println();
  9.   test.testLoopTwo();
  10.   
  11.   int a [] = new int[10];
  12.   a[0]=2;
  13.   a[1]=4;
  14.   a[2]=22;
  15.   a[3]=3;
  16.   a[4]=9;
  17.   a[5]=12;
  18.   a[6]=11;
  19.   a[7]=6;
  20.   a[8]=5;
  21.   a[9]=0;
  22.   
  23.   Arrays.sort(a);
  24.   
  25.  }
  26.  /**
  27.   * 使用標誌控制
  28.   */
  29.  public  void testLoopOne ()
  30.  {
  31.   HH:
  32.    for(int i=0;i<10;i++)
  33.    {
  34.     System.out.println("i的數值是:"+i);
  35.     for(int k=0;k<10;k++)
  36.     {
  37.      System.out.println("k的數值是:"+k);
  38.      if(k==6)
  39.      break HH;
  40.     }
  41.    }
  42.      
  43.       System.out.println("跳出所有迴圈......");
  44.  }
  45.  /**
  46.   * 使用標旗量控制
  47.   */
  48.  public void testLoopTwo ()
  49.  {
  50.   
  51.   int tmp = 0;
  52.   for(int i=0;i<10;i++)
  53.   {
  54.    System.out.println("i的數值是::::::"+i);
  55.    for(int k=0;k<10;k++)
  56.    {
  57.     System.out.println("k的數值是::::::"+k);
  58.     tmp=k;
  59.     if(k==6)
  60.      break;   
  61.    }
  62.    if(tmp==6)
  63.     break;
  64.   }
  65.   System.out.println("跳出所有迴圈......");
  66.  }
  67. }

        從他的問題,給我敲了一警鐘,以後學東西真的要仔細點了,細心啊……謹記!

聯繫我們

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