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)的内容。