相关疑难解决方法(0)

如何使用BigInteger?

我有这段代码,但是没有用:

BigInteger sum = BigInteger.valueOf(0);
for(int i = 2; i < 5000; i++) {
    if (isPrim(i)) {
        sum.add(BigInteger.valueOf(i));
    }
}
Run Code Online (Sandbox Code Playgroud)

sum变量总是0.我做错了什么?

java biginteger

150
推荐指数
5
解决办法
33万
查看次数

BigInteger难度大

我试图用Recursion和BigIntegers做Factorial,但eclipse抱怨BigInteger.我知道这个程序本来应该很简单,但它给我带来了麻烦.这是代码.

import java.util.Scanner;
import java.math.BigInteger;

public class Factorial
{
    public static void main(String[] args) 
    {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter integer");
        BigInteger n = input.nextBigInteger();
        System.out.println("Factorial of " + n + " is "  + fact(n));

    }

    public static  int fact(BigInteger n)
    {
        if(n ==0)
        {
            return 1;
        }
        else
        {
            return n * fact(n-1);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

java biginteger factorial

2
推荐指数
1
解决办法
469
查看次数

标签 统计

biginteger ×2

java ×2

factorial ×1