【发布时间】:2017-09-19 00:05:40
【问题描述】:
我有一个类似于下面给出的 for 循环。
for(int i=0; i<10; i++)
{
boolean condition = checkCondition(); /* line 3 */
if(condition)
{
if(some other condition A)
{
move to line 3;
}
else if(some other condition B)
{
call_method_B();
}
else
{
call_method_C();
}
}
else
{
call_method_D();
}
}
如何使程序返回到上述 if 语句中的第 3 行?我不想破坏迭代。需要在同一个迭代中,只需要回到第 3 行。
【问题讨论】:
-
你需要一个循环。
-
我相信,如果你有一些基本条件,你可以更喜欢递归方法,而不是使用迭代方法
-
是的,我知道我需要一种递归方法。我不能像 shmosel 建议的那样使用循环,因为我需要的功能是递归的。但我无法弄清楚该怎么做。你有什么想法吗?
-
@zim :我已经使用递归方法发布了答案。您可以参考该方法。
-
你可以使用 break / goto 标签吗?
标签: java for-loop if-statement