2 回答

TA貢獻1831條經驗 獲得超10個贊
嘗試這個。每次運行后,取消注釋其中一個遞歸調用。
public class ForEverDemo {
static long count = 0;
public static void main(String[] args) {
alongtime(0);
System.out.println("count = " + count);
}
public static void alongtime(int v) {
count++;
if (v == 9) {
return;
}
for (int i = 0; i < 3; i++) {
alongtime(v + 1);
// alongtime(v + 1);
// alongtime(v + 1);
// alongtime(v + 1);
// alongtime(v + 1);
}
}
}

TA貢獻1998條經驗 獲得超6個贊
終count有其價值9。通過打印這個值來檢查這個。
static void calc(int prev, int count) {
System.out.println("count=" + count + " prev=" + prev);
if (count == 9) {
return;
}
...
你的遞歸中有很多分支,需要大量的計算。這就是為什么它沒有結束。你可以再等一個小時或幾天/幾年才能看到這個程序的結束?;蛘哂糜行У拇a替換此代碼。
添加回答
舉報