使用Recursion时返回void方法中的函数

时间:2014-03-31 08:24:09

标签: java recursion return void

我目前正在从一本书中学习Java的基础知识,并且我已将此代码作为使用Recursion的嵌套循环的示例。我理解一切,但在代码的最后使用return函数。我无法弄清楚程序是如何决定的,何时在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();
}
}

如果有人向我解释,当数字为&#34; 4.4&#34;时,这个特定的例子中的返回是如何工作的将是非常有帮助的。以及它在虚空方法中的运作方式,因为我一直在寻找解释但没有成功...

事先谢谢!

2 个答案:

答案 0 :(得分:0)

nestedLoops方法中的for循环调用自己numberOfIterations次。所以它变为0,然后进行numberOfIterations次调用。因此,如果您输入4,您将看到4个使用currentLoop = 1的调用,然后使用currentLoop = 2调用16个调用....等等....

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

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

答案 1 :(得分:0)

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(我们在最开始输入的值)所以这是假的,并且没有调用里面的代码。 / p>

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方法中的返回只是意味着“好吧,停止你正在做的事情并回到谁/什么叫这个并继续前进”在这种情况下它跳回到前面的循环以继续工作。