[英]Trouble Understanding the Recursion Here
有人可以帮我解释一下为什么这会导致无限递归循环吗?
可变长度达到值1,但是由于某种原因,即使循环条件为while(length> 1),仍会进入循环。
我尝试过打印值并一遍又一遍地运行它,也许我错过了一些更明显的东西,或者有人可以更简单地解释这一点。 谢谢。
public static void main(String[] args) {
xMethod(5);
}
public static void xMethod(int length) {
while (length > 1) {
System.out.print((length - 1) + " ");
xMethod(length - 1);
}
}
附加信息。
当我调试此代码时:
public static void main(String[] args) {
xMethod(5);
}
public static void xMethod(int length) {
while (length > 1) {
System.out.print((length - 1) + " ");
xMethod(length - 1);
}
System.out.println("Coming out of while");
}
以下是输出:
4 3 2 1 Coming out of while
1 Coming out of while
1 Coming out of while
1 Coming out of while
1 Coming out of while
1 Coming out of while
1 Coming out of while
//repeated Infinite times
从while
循环出来后while
为什么要返回相同的while
循环, length
为2?
编辑:我感谢您的所有答复,并且理解,如果我想像这样编写代码,我可能会像大多数递归方法一样使用if语句,但这只是我的一个问题,也许是我不了解范围或调用堆栈的工作原理。 如果我是正确的,那么无论该循环在块外发生了什么,while循环块都将length的值保持为2。
因为您没有在当前方法中更新length的值。 发送到方法时,该值仅递减。
public static void main(String[] args) {
xMethod(5);
}
public static void xMethod(int length) {
while (length > 1) {
System.out.print((length) + " ");
xMethod(length);
length--;
}
}
可变长度在任何循环中都永远不会达到值1,您混合使用两种设计,我认为您需要使用它们,即递归方法或循环。
第一个设计:
public static void main(String[] args) {
xMethod(5);
}
public static void xMethod(int length) {
System.out.print((length - 1) + " ");
if(length > 1)
xMethod(length - 1);
}
}
其他方式:
public static void main(String[] args) {
xMethod(5);
}
public static void xMethod(int length) {
while (length > 1) {
System.out.print((length--) + " ");
}
}
您可以选择其中之一,这取决于您的设计。 如果不是您的答案,请写出您的期望输出。
您正在这里做两件事。 在编写递归代码时,您始终需要考虑何时代码何时结束。 您的代码没有结尾。
public static void main(String[] args) {
xMethod(5);
}
public static void xMethod(int length) {
System.out.println("Method Start "+ length);
while (length > 1) {
System.out.println("Inside while "+ length);
xMethod(length - 1);
}
System.out.println("Method End "+ length);
}
}
现在,此代码将产生以下输出:
Method Start 5
Inside while 5
Method Start 4
Inside while 4
Method Start 3
Inside while 3
Method Start 2
Inside while 2
Method Start 1
Method End 1
Inside while 2
Method Start 1
Method End 1
Inside while 2
Method Start 1
Method End 1
Inside while 2
Method Start 1
Method End 1
.
.
如您所见,
Inside while 2
Method Start 1
Method End 1
一次又一次地重复。
所以这意味着,当长度为2时,将发生以下情况。
while (2 > 1) {
System.out.println("Inside while "+ length);
xMethod(1);
}
输出是
Inside while 2
现在, xMethod(1)
甚至都没有进入while循环,因此它将被打印出来。
Method Start 1
Method End 1
但是,您现在应该了解,再次执行了while(2>1)
,因为长度没有改变,并且仍然为2
。
while (2 > 1){
System.out.println("Inside while "+ length);
xMethod(1);
}
继续,循环继续。
因为当length
达到2
xMethod(2)
并因此xMethod(1)
,所以当xMethod(1)
结束时,由于length
仍为2
它将再次调用xMethod(2)
并再次调用xMethod(1)
。
要修复它,请在xMethod(length - 1);
之后使用return
xMethod(length - 1);
public static void main(String[] args){
xMethod(5);
}
public static void xMethod(int length) {
while (length > 1) {
System.out.print((length - 1) + " ");
xMethod(length - 1);
return;
}
System.out.println("Coming out of while");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.