程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

2023-06(1)

Java 实例 - 标签(Label)

发布于2021-01-01 15:54     阅读(1208)     评论(0)     点赞(21)     收藏(2)


Java 中的标签是为循环设计的,是为了在多重循环中方便的使用 break 和coutinue 。

以下实例当在循环中使用 break 或 continue 循环时跳到指定的标签处:

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(“字符串中没有发现子字符串。”);
}
}
}
以上代码运行输出结果为:

发现子字符串。

原文链接:https://blog.csdn.net/weixin_43055166/article/details/111994251



所属网站分类: 技术文章 > 博客

作者:lovejava

链接:http://www.javaheidong.com/blog/article/45797/8da665b1aa0ab9aa70f2/

来源:java黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

21 0
收藏该文
已收藏

评论内容:(最多支持255个字符)