您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
在Java中,continue
关键字用于跳过当前循环迭代中剩余的代码,并开始下一次迭代。在while
循环中,当满足某个条件时,可以使用continue
来跳过当前迭代。
下面是一个简单的示例,演示了如何在while
循环中使用continue
关键字:
public class ContinueExample {
public static void main(String[] args) {
int counter = 0;
while (counter < 10) {
counter++;
// 如果counter是偶数,跳过当前迭代
if (counter % 2 == 0) {
continue;
}
System.out.println("Counter: " + counter);
}
}
}
在这个示例中,我们使用while
循环从1计数到10。当counter
是偶数时,我们使用continue
关键字跳过当前迭代,不执行System.out.println("Counter: " + counter);
语句。因此,输出结果将是:
Counter: 1
Counter: 3
Counter: 5
Counter: 7
Counter: 9
可以看到,所有偶数都被跳过了。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。