内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用
在Java中退出/终止while循环的最佳方式是什么?
例如,我的代码目前如下所示:
while(true){ if(obj == null){ // I need to exit here } }
使用break:
break
while (true) { .... if (obj == null) { break; } .... }
但是,如果您的代码看起来与您指定的完全相同,则可以使用正常while循环并将条件更改为obj != null:
while
obj != null
while (obj != null) { .... }
while(obj != null){ // statements. }
扫码关注云+社区