不兼容的类型:BigInteger 无法转换为 int

incompatible types: BigInteger cannot be converted to int

我正在尝试 运行 此代码,但出现无法解决的错误。请帮忙。

import java.util.Scanner;
import java.math.BigInteger;
class Cseq
{
    public static void main(String []args)
    {
        Scanner jais=new Scanner(System.in);
        int x=100000;
        BigInteger i;
        BigInteger []a = new BigInteger[x];
        a[0]=BigInteger.ONE;
        for(i=BigInteger.ONE;i.compareTo(BigInteger.valueOf(x))<=0;i=i.add(BigInteger.ONE))
        {
            a[i]=a[(i.subtract(BigInteger.ONE))].multiply(i);
        }
        int t=jais.nextInt();
        while(t--!=0)
        {
        BigInteger n=jais.nextBigInteger();
        BigInteger p=jais.nextBigInteger();
        BigInteger q=jais.nextBigInteger();
        BigInteger v=(q.subtract(p)).add(BigInteger.ONE);
        BigInteger j;
        BigInteger sum=BigInteger.ZERO;
        for(j=BigInteger.ONE;j.compareTo(n)<=0;j=j.add(BigInteger.ONE))
        {
            sum=sum.add(a[v].divide(a[(v.subtract(j))].multiply(a[j])));
            sum=sum.add(v);
        }
        sum=sum.subtract(v);
        System.out.println(sum);
        }
        jais.close();
    }
}

您收到 5 个错误,均与数组的索引值有关。那应该是整数。

在所有这些表达式中,您需要将表达式结果更改为整数值。

a[i]=a[(i.subtract(BigInteger.ONE))].multiply(i);

这里i(i.subtract(BigInteger.ONE))应该是整数。

还有

sum=sum.add(a[v].divide(a[(v.subtract(j))].multiply(a[j])));

这里 v , (v.subtract(j))j 应该是整数。

相应地更改这些索引数据类型。 在所有这些地方使用 intValue() look it here

更改这行代码

a[i]=a[(i.subtract(BigInteger.ONE))].multiply(i);

a[i.intValue()]=a[(i.subtract(BigInteger.ONE)).intValue()].multiply(i);