递归中的无限循环

时间:2014-08-20 03:23:50

标签: java recursion infinite-loop

我将一个代码放在一起输出以下模式:

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);
    }
}

4 个答案:

答案 0 :(得分:7)

你的算法略有偏差,你不应该有静态变量,你不应该改变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

答案 1 :(得分:0)

您根本不需要startindex - 您只需要参数a来表示递归的暂停条件。对于recusion的每次递归调用,a都会少一次,a == 0就会打破递归。

按原样,您每次都会将startindex重置为1,因此违规情况始终为假。

public static void recursion(int a, int b) {
    if (a == 0) {
       return;
    }

    // Print here

    recursion(a - 1, b + 1);
}

答案 2 :(得分:0)

你是在递归方法的开始重置变量startindex,所以如果调用递归方法并且startindex增加1然后再次调用该方法,则由于int startindex = 1,变量将被重置;

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);
}
}

答案 3 :(得分:0)

您的问题是每次调用函数时都会重置启动索引。试试这段代码

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