簡體   English   中英

使用遞歸時void方法中的返回函數

[英]Return function in void method when using Recursion

我目前正在從一本書中學習 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();
}
}

如果有人向我解釋當數字為“4.4”時 return 在這個特定示例中的最終工作原理以及它在 void 方法中的工作原理,那將非常有幫助,因為我一直在尋找對此的解釋,但沒有成功...

先謝謝了!

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

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 方法中的 return 僅表示“好吧,停止你正在做的事情,回到誰/什么人那里並繼續前進”在這種情況下,它跳回前一個 for 循環以繼續工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM