[英]Infinite loop in recursion
我正在整理一個代碼來輸出以下模式:
000000000X
00000000XX
0000000XXX
000000XXXX
00000XXXXX
0000XXXXXX
000XXXXXXX
00XXXXXXXX
0XXXXXXXXX
(每一行應該是一個接一個。我不太確定如何在論壇上顯示模式...對不起)
我應該在代碼內使用遞歸循環,但最終會陷入無限循環,我真的不明白為什么。(可以確定我實際上從沒有使用過遞歸循環)。是我的代碼:
class Recursion {
//recursion should stop after 9 attempts
static int stopindex = 9;
public static void main(String[] args) {
//a=number of "O"s and b=number of "X"s
int a = 9;
int b = 1;
recursion(a, b);
}
public static void recursion(int a, int b) {
//start of recursion at index 1
int startindex = 1;
//stop condition of recursion
if (startindex == stopindex)
return;
//printing of pattern
for (int i = a; i > 0; i--) {
System.out.print("O");
}
for (int j = 0; j < b; j++) {
System.out.print("X");
}
System.out.println();
--a;
++b;
++startindex;
recursion(a, b);
}
}
你的算法略有偏差,你不應該有靜態變量,你不應該改變a,你的第一個for循環條件 - 我想你想要的,
public static void recursion(int a, int b) {
// stop condition of recursion
if (a == b) return;
// printing of pattern
for (int i = a - b; i > 0; i--) {
System.out.print("O");
}
for (int j = 0; j < b; j++) {
System.out.print("X");
}
System.out.println();
// --a;
++b; // <-- this could be done in the recursion call below,
recursion(a, b);
// recursion(a, ++b); // <-- like that.
}
輸出是
OOOOOOOOX
OOOOOOOXX
OOOOOOXXX
OOOOOXXXX
OOOOXXXXX
OOOXXXXXX
OOXXXXXXX
OXXXXXXXX
您根本不需要startindex
參數a
就是您為遞歸制定暫停條件所需要的。 對於每次遞歸的recusion
調用, a
是一個少於一個,一旦a == 0
你就打破了遞歸。
因此,您每次都將startindex
重置為1
因此斷開條件始終為false。
public static void recursion(int a, int b) {
if (a == 0) {
return;
}
// Print here
recursion(a - 1, b + 1);
}
您要在遞歸方法的開始處重置變量startindex,因此,如果int startindex = 1 ;,則調用遞歸方法並將startindex增加一,然后再次調用該方法,變量將被重置。
class Recursion {
static int stopindex = 9;
int startindex = 1;
public static void main(String[] args) {
//a=number of "O"s and b=number of "X"s
int a = 9;
int b = 1;
recursion(a, b);
}
public static void recursion(int a, int b) {
//start of recursion at index 1
//int startindex = 1; - removed due to variable reset
//stop condition of recursion
if (startindex == stopindex)
return;
//printing of pattern
for (int i = a; i > 0; i--) {
System.out.print("O");
}
for (int j = 0; j < b; j++) {
System.out.print("X");
}
System.out.println();
--a;
++b;
++startindex;
recursion(a, b);
}
}
您的問題是每次調用函數時都會重置啟動索引。 試試這個代碼
class Recursion {
//recursion should stop after 9 attempts
static int stopindex = 9;
public static void main(String[] args) {
//a=number of "O"s and b=number of "X"s
int a = 9;
int b = 1;
int startindex = 1;
recursion(a, b, startindex);
}
public static void recursion(int a, int b, int startIndex) {
//start of recursion at index 1
//stop condition of recursion
if (startIndex > stopindex)
return;
//printing of pattern
for (int i = a; i > 0; i--) {
System.out.print("O");
}
for (int j = 0; j < b; j++) {
System.out.print("X");
}
System.out.println();
--a;
++b;
++startIndex;
recursion(a, b, startIndex);
}
}
產量
OOOOOOOOOX
OOOOOOOOXX
OOOOOOOXXX
OOOOOOXXXX
OOOOOXXXXX
OOOOXXXXXX
OOOXXXXXXX
OOXXXXXXXX
OXXXXXXXXX
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.