Java 實(shí)例 - 標(biāo)簽(Label)
Java 中的標(biāo)簽是為循環(huán)設(shè)計(jì)的,是為了在多重循環(huán)中方便的使用break 和coutinue 。
以下實(shí)例當(dāng)在循環(huán)中使用 break 或 continue 循環(huán)時(shí)跳到指定的標(biāo)簽處:
/* author by w3cschool.cn 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("發(fā)現(xiàn)子字符串。"); } else { System.out.println("字符串中沒有發(fā)現(xiàn)子字符串。"); } } }
以上代碼運(yùn)行輸出結(jié)果為:
發(fā)現(xiàn)子字符串。
更多建議: