【问题标题】:Infinite loop in recursion递归中的无限循环
【发布时间】:2014-10-13 07:28:03
【问题描述】:

我正在整理一个代码以输出以下模式:

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

【问题讨论】:

    标签: java recursion infinite-loop


    【解决方案1】:

    你的算法有点不对劲,你不应该有静态变量,你不应该改变 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
    

    【讨论】:

    • 很好地使用 for 循环...以前从来没有这样用过。再次感谢。
    • @user3889963 如果您的问题得到解答,请accept
    【解决方案2】:

    您在递归方法开始时重置变量 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】:

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

      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
      

      【讨论】:

        【解决方案4】:

        您根本不需要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);
        }
        

        【讨论】:

          猜你喜欢
          • 2016-10-06
          • 1970-01-01
          • 2012-10-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-02-08
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多