【问题标题】:Return function in void method when using Recursion使用递归时 void 方法中的返回函数
【发布时间】:2014-05-10 12:13:11
【问题描述】:

我目前正在从一本书中学习 Java 的基础知识,并且我将这段代码作为使用递归的嵌套循环的示例。我什么都懂,但代码末尾的返回函数的用法。我无法弄清楚程序是如何决定的,何时在 K=4 时准确停止。我试图调试它,这对我来说仍然是个谜。这是代码:

import java.util.Scanner;

public class nestedLoops {
public static int numberOfLoops;
public static int numberOfIterations;
public static int[] loops;

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    System.out.print("N = ");
    numberOfLoops = input.nextInt();

    System.out.print("K = ");
    numberOfIterations = input.nextInt();

    input.close();

    loops = new int[numberOfLoops];

    nestedLoops(0);
}

public static void nestedLoops(int currentLoop) {
    if (currentLoop == numberOfLoops) {
        printLoops();
        return;
    }

    for (int counter=1;counter<=numberOfIterations;counter++) {
        loops[currentLoop] = counter;
        nestedLoops(currentLoop + 1);
    }
}

public static void printLoops() {
    for (int i = 0; i < numberOfLoops; i++) {
        System.out.printf("%d ", loops[i]);
    }
    System.out.println();
}
}

如果有人向我解释 return 在这个特定示例中最终在数字为“4.4”时的工作原理以及它在 void 方法中的工作原理,那将非常有帮助,因为我一直在寻找对此的解释但没有成功……

先谢谢你!

【问题讨论】:

  • 在使用return语句的void方法中,方法将结束。
  • 对,我知道这一点。但是,如果您调试此代码,您会发现它多次将值“currentLoop”更改为 2,而不是在特定时刻程序决定它将是最后一次,实际上给出了答案,我不明白它是如何实现的......

标签: java recursion return void


【解决方案1】:

void 方法中的 return 语句停止运行该方法并返回到调用代码。在这个带有输入的例子中:

numberOfLoops = 4
numberOfIterations = 4

在获取输入之后,根据输入创建一个数组,然后调用nestedLoops(0) 方法:

public static void nestedLoops(int currentLoop) {
    if (currentLoop == numberOfLoops) {
        printLoops();
        return;
    }

    for (int counter=1;counter<=numberOfIterations;counter++) {
        loops[currentLoop] = counter;
        nestedLoops(currentLoop + 1);
    }
}

解释

对于开始,让我们忽略 for 循环。 if 语句检查是否currentLoop == numberOfLoops 并且每次调用此方法时都会执行此操作。现在currentLoop 是 0(我们在调用它时传递给这个方法的值),而 numberOfLoops 是 4(我们在一开始输入的值)所以这是假的,里面的代码都没有被调用。

if 语句下方的 for 循环将运行 numberOfIterations 次。在我们的例子中,这个循环将运行 4 次。我将按顺序写出下面发生的事情:

 - input is 4, 4
 - nestedLoops(0) called- currentLoop = 0
 - if evaluates to false
 - for loop runs
     - loops[0] = 1
     - nestedLoops(1)
     - if evaluates to false ( 1 != 4)
     - for loop runs
          - loops[1] = 1
          - nestedLoops(2)
          - if evaluates to false (2 != 4)
          - for loop runs
              - loops[2] = 1
              - nestedLoops(3)
              - if evaluates to false (3 != 4)
              - for loop runs
                  - loops[3] = 1
                  - nestedLoops(4)
                  - if evaluates to TRUE (4 == 4)
                  - loops are printed (all values are 1 right now)
                  -returns to calling location
              -Which is the for loop associated with this indention. 
              -For loop increments, and then sets loops[3] = 2.
           - then this loop finishes
       - then this loop finishes
 etc. etc.

void 方法中的返回仅表示“好的,停止你正在做的事情并返回到调用此方法的人/任何人并继续前进”在这种情况下,它会跳回上一个 for 循环以继续工作。

【讨论】:

  • 伙计,谢谢你的简短解释。如果您能向我解释一下代码的最后部分,我将不胜感激:程序如何决定停止重新启动“计数器”,但最终完成并返回 main 方法?
【解决方案2】:

nestedLoops 方法内的 for 循环调用自身 numberOfIterations 次。所以它变为 0,然后进行 numberOfIterations 调用。因此,如果您输入 4,您会看到 4 个调用 currentLoop=1,然后看到 16 个调用 currentLoop=2,依此类推....

当所有其他方法都失败时,编写一些代码来调试您的代码。我自己就是一个视觉人,所以当调试器为我做一些输出时会有所帮助。

public static HashMap<Integer, Integer> map = new HashMap();

public static void main(String[] args) {
    ....
    System.out.println(map);
}

public static void nestedLoops(int currentLoop) {
    if(map.containsKey(currentLoop)) {
        map.put(currentLoop, map.get(currentLoop)+1);
    } else {
        map.put(currentLoop, 1);
    }
    ...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-16
    • 2013-07-05
    • 1970-01-01
    • 2022-01-16
    • 1970-01-01
    相关资源
    最近更新 更多