java 我将如何完全重新启动 for 循环或 if 语句?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30180180/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How would I completely restart a for loop or an if statement?
提问by Jesse Wiatrak
I am looking for an answer to EITHER of these two questions because either one will work for my code. Is there a function or something that will completely restart a for
loop or rerun an if
statement? For the for
loop, let's say it is
我正在寻找这两个问题的答案,因为任何一个都适用于我的代码。是否有一个函数或东西可以完全重新启动for
循环或重新运行if
语句?对于for
循环,假设它是
for(int i = 0; i<=30; i++){
//code here
}
and by this point it had gotten to i = 5
, how would I tell it to go back to i = 0
?
到这个时候它已经到了i = 5
,我怎么告诉它回去i = 0
?
For the if
statement, let's say the if
statement was:
对于if
声明,假设if
声明是:
if(i == 1 && j == 2){
//more code here
}
How would I tell it, at the end of the if
statement, if the if
statement is true
, to completely rerun the if
statement?
在if
语句的末尾,如果if
语句是true
,我将如何告诉它完全重新运行if
语句?
回答by elixenide
TL;DRIt sounds like you want a while
loop or possibly a recursive function.
TL;DR听起来你想要一个while
循环或者可能是一个递归函数。
The for
Loop Version
该for
环路版本
You can't restart an if
statement, but you can modify your loop control variable in the for
loop. Just do i = 0;
and watch out for infinite loops, like this:
您不能重新启动if
语句,但可以在循环中修改循环控制变量for
。只需i = 0;
注意无限循环,就像这样:
for(int i = 0; i < 30; i++) {
// do stuff
if(someCondition) {
i = 0;
}
}
This isn't very good style, though, and can lead to some devilish bugs that are hard to find. People aren't used to looking for modifications to the loop control variable inside the for
loop because it's one of those things that You. Just. Don't. Do.
但是,这不是很好的样式,并且可能会导致一些难以找到的邪恶错误。人们不习惯在循环内寻找对循环控制变量的修改,for
因为它是 You. 只是。别。做。
The while
Loop Version
该while
环路版本
So, instead of a for
loop, you should use a while
loop if you plan to modify i
inside the loop.
因此,如果您计划在循环内进行修改for
,则应使用while
循环而不是i
循环。
Example:
例子:
int i = 0;
while(i < 30) {
// do stuff
if(someCondition) {
i = 0;
} else {
i++;
}
}
The Recursive Function Version
递归函数版本
Depending on what you really want to do, you might also consider creating a function and calling it recursively:
根据您真正想要做什么,您还可以考虑创建一个函数并递归调用它:
function myFunction() {
// do stuff
if(someCondition) {
myFunction(); // this is the recursion
}
}
for(int i = 0; i < 30; i++) {
myFunction();
}
With all of these approaches, watch out for infinite loops! They are easy to create if you aren't careful.
使用所有这些方法,注意无限循环!如果您不小心,它们很容易创建。
回答by Pranav Totla
Typically you can use continue
and break
statements for your problem, but they won't initialize back to i = 0
.
通常,您可以针对您的问题使用continue
andbreak
语句,但它们不会初始化回i = 0
.
However, for your problem the following is suitable:
但是,对于您的问题,以下是合适的:
for(i=0; i<30; i++){
if(condition check) {i=0;}
// other code
}
回答by Lance
Be careful doing this. You can get stuck in a loop you can't get out of.
做这件事要小心。您可能会陷入无法摆脱的循环中。
for(int i = 0; i<=30; i++){
//code here
if (some condition) {i=0;}
}
回答by epedos
All the other answers are incorrect, because on the first iteration i=0;
sets i
to 0
and then i++
in the for loop increases it to 1
, so basically you never start from i=0
again, which will result in always skipping the first array element if you want to check an Array a[i]
.
所有其他答案都不正确,因为在第一次迭代中i=0;
设置i
为0
,然后i++
在 for 循环中将其增加为1
,因此基本上您永远不会重新开始i=0
,如果您想检查 Array ,这将导致总是跳过第一个数组元素a[i]
。