小编shu*_*vro的帖子

Java中带有BigInteger的StackOverFlowError

为什么此Java代码会引发StackOverflowError异常?

public class factorial2 {

     public BigInteger fact( BigInteger n)
     {
         BigInteger one = new BigInteger("1");
         if(n.equals("0"))
              return one;
         else
             return n.multiply(fact(n.subtract(one)));      
     }

     public static void main(String[] args) {       
        @SuppressWarnings("resource")
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        factorial2 f = new factorial2();
        for(int i=0;i<n;i++)
        {
           BigInteger b = sc.nextBigInteger();
           System.out.println(f.fact(b));
        }
        sc.close();
    }
}
Run Code Online (Sandbox Code Playgroud)

我尝试使用生成阶乘BigInteger。但是,为什么我的代码在输入时给出引用异常?

java stack-overflow recursion biginteger

4
推荐指数
1
解决办法
512
查看次数

标签 统计

biginteger ×1

java ×1

recursion ×1

stack-overflow ×1