Why is this code sent to me stackoverflow in Java

I tried to use long and double with C, K, n variables, but NetBeans showed a stack overflow error:

public class Main {

public static void main(String[] args) {
   double c=0; //combinatorial
   double n=5;
   double k=15;

   c= factorial(n)/(factorial(k)*factorial(n-k));

   System.out.print(n+" combinatorial "+k+" between "+c+"\n");

}

    static double factorial (double number) {
        if (number == 0) 
            return 1;
          else 
            return number * factorial(number-1);
   }
}

Exception in thread "main" java.lang.StackOverflowError
    at co.combinatorial.Main.factorial(Main.java:26)
    at co.combinatorial.Main.factorial(Main.java:29)
    at co.combinatorial.Main.factorial(Main.java:29)
    at co.combinatorial.Main.factorial(Main.java:29)
......

Java result: 1

Do I have to use integer text or long parselong

What did I do wrong?

Solution

From the initial value, n-k = - 10 Since this is less than 0, your factorial method will never return

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>