Java 实例 - continue 关键字用法

3187 1
LZH实名认证 2022-3-20 13:44:54 | 显示全部楼层 |阅读模式
Java continue 语句语句用来结束当前循环,并进入下一次循环,即仅仅这一次循环结束了,不是所有循环结束了,后边的循环依旧进行。

以下实例使用了 continue 关键字来跳过当前循环并开始下一次循环:

Main.java 文件
public class Main {
    public static void main(String[] args) {
        StringBuffer searchstr = new StringBuffer("hello how are you. ");
        int length = searchstr.length();
        int count = 0;
        for (int i = 0; i < length; i++) {
            if (searchstr.charAt(i) != 'h')
            continue;
            count++;
            searchstr.setCharAt(i, 'h');
        }
        System.out.println("发现 " + count
        + " 个 h 字符");
        System.out.println(searchstr);
    }
}
以上代码运行输出结果为:

发现 2 个 h 字符
hello how are you.
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2026 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行