在Java中退出/终止while循环的最佳方法是什么?
例如,我的代码当前如下:
while(true){
if(obj == null){
// I need to exit here
}
}
用途break
:
while (true) {
....
if (obj == null) {
break;
}
....
}
但是,如果您的代码看起来完全像您指定的那样,则可以使用普通while
循环并将条件更改为obj != null
:
while (obj != null) {
....
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句