有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java很难理解这里的递归

有人能帮我解释一下为什么这最终会变成一个无限递归循环吗

可变长度达到值1,但由于某种原因,即使循环条件为while(长度>;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循环后,为什么它会回到与length相同的while循环中,与length作为2

编辑:我感谢您的所有回复,并且理解如果我想编写这样的代码,我可能会像大多数递归方法一样使用if语句,但这只是我可能不理解作用域或调用堆栈如何工作的问题。如果我是正确的,while循环块将保持长度为2的值,无论在该块之外发生什么


共 (4) 个答案

  1. # 1 楼答案

    因为您没有在当前方法中更新长度的值。发送到方法时,该值只是递减

    public static void main(String[] args) {
        xMethod(5);
    }
    
    public static void xMethod(int length) {
        while (length > 1) {
            System.out.print((length) + " ");
            xMethod(length);
            length--;
        }
    }
    
  2. # 2 楼答案

    因为当length到达2{}被调用,因此xMethod(1)接着,当xMethod(1)结束时,因为length仍然是2,它再次调用xMethod(2),而这个调用xMethod(1)它重复

    要修复它,请在xMethod(length - 1);之后使用return

    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");
        }
    
  3. # 3 楼答案

    你在这里做两件事。在编写递归代码时,始终需要考虑代码何时结束。你的代码没有结尾

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

    继续,循环继续

  4. # 4 楼答案

    变量长度在任何循环中都不会达到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--) + " ");
        }
    }
    

    你可以选择其中之一,这取决于你的设计。 如果不是你的答案,请写下你的预期输出