import java.math.BigInteger;
import java.util.Arrays;
import java.util.Scanner;
public class Java {
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];
System.out.println("main START");
nestedLoops(0);
System.out.println("main END");
}
public static void nestedLoops(int currentLoop) {
System.out.println("nestedLoops");
System.out.println("currentLoop " + currentLoop);
if (currentLoop == numberOfLoops) {
printLoops();
return;
}
for (int counter = 1; counter <= numberOfIterations; counter++) {
System.out.println("nestedLoops in LOOP");
System.out.println("currentLoop in LOOP " + currentLoop);
loops[currentLoop] = counter;
nestedLoops(currentLoop + 1);
}
}
public static void printLoops() {
System.out.println("printLoops");
for (int i = 0; i < numberOfLoops; i++) {
System.out.printf("%d", loops[i]);
}
System.out.println();
}
}
大家好。我是新來的,這是我的第一篇文章。調試一步一步遞歸
我的問題是:
如果我把爲N = 2和K = 4,爲什麼後首次返回currentLoop繼續1中,我們傳遞給方法0?
感謝,尼古拉
我不明白.. – Maroun
請更明確地詢問你在問什麼。即說明你期望在哪裏發生,然後告訴我們你的想法實際發生了什麼,以及爲什麼這不符合你的期望。 –