所以当我设置“休息”时; while 循环末尾的语句,或者当条件设置为 false 时,有没有办法跳回循环顶部并再次执行它?
我知道“继续”;陈述。它跳回 while 循环的顶部,但仅当它在该循环内部声明时才有效。
有没有办法在完全退出循环后达到同样的效果?
while(true) {
//code
break;
}
int i=1;
if(i=1) {
//command to go back into the while loop
}
这似乎可以通过将代码拆分为方法并重用它们来解决?
void executeLoop() {
while(true) {
//code
break;
}
}
void doStuff() {
executeLoop();
int i=1;
if(i==1) { // Added a = symbol here to compare instead of assigning
executeLoop();
}
}
试试这个。
您可以使用 goto() 标签并在循环顶部添加一个标签。
标签: While 循环 转到标签;