那麼,我已經做了一個程序,可以找到一個數字槽遞歸的階乘。它工作正常,但增量存在問題。看,如果我這樣寫這個程序,它不起作用! -Java堆棧預遞增器遞歸溢出
package programming.tutorialnext;
import java.util.Scanner;
public class Factorial_Recursion {
public static int factorial(int n) {
if (n == 1) {
return n;
} else {
return n * factorial(n--);
}
}
public static void main(String[] args) {
Scanner bucky = new Scanner(System.in);
int n;
System.out.print("Enter a number for it's factorial :");
n = bucky.nextInt();
System.out.print("This is it's factorial : " + factorial(n));
}
}
它說,由於某種原因堆棧溢出,即使沒有。 = 3! 但是,如果我使用這樣的預增量器: - n在頂部,它工作正常!
你八九不離十提出和回答問題的同時... – Ubica
使用'N *階乘( - N);',而不是'N *階乘(N - );' – iMBMT