如何在Java中退出while循环?

BalaB:

在Java中退出/终止while循环的最佳方法是什么?

例如,我的代码当前如下:

while(true){
    if(obj == null){

        // I need to exit here

    }
}
dacwe:

用途break

while (true) {
    ....
    if (obj == null) {
        break;
    }
    ....
}

但是,如果您的代码看起来完全像您指定的那样,则可以使用普通while循环并将条件更改为obj != null

while (obj != null) {
    ....
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章