[英]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.