首頁  >  文章  >  Java  >  Java 實例 - 標籤(Label)

Java 實例 - 標籤(Label)

黄舟
黄舟原創
2017-02-16 10:24:301117瀏覽

Java 中的標籤是為循環設計的,是為了在多重循環中方便的使用break 和coutinue 。

以下實例當在循環中使用break 或continue 循環時跳到指定的標籤處:

/*
 author by w3cschool.cc
 Main.java
 */public class Main {
   public static void main(String[] args) {
      String strSearch = "This is the string in which you have to search for a substring.";
      String substring = "substring";
      boolean found = false;
      int max = strSearch.length() - substring.length();
      testlbl:
      for (int i = 0; i <= max; i++) {
         int length = substring.length();
         int j = i;
         int k = 0;
         while (length-- != 0) {
            if(strSearch.charAt(j++) != substring.charAt(k++)){
               continue testlbl;
            }
         }
         found = true;
         break testlbl;
      }
      if (found) {
         System.out.println("发现子字符串。");
      }
      else {
         System.out.println("字符串中没有发现子字符串。");
      }
   }}

 

以上程式碼運行輸出結果為:

发现子字符串。

 以上是Java 實例- 標籤(Label)的內容,更多相關內容請關注PHP中文網(www.php.cn)!


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn