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

问题描述:

我想运行这段代码,但我得到错误,我无法解决。请帮忙。不兼容的类型:BigInteger不能转换为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(); 
    } 
} 
+0

http://ideone.com/Lq7KX7这里是链接到这个问题.. – 2015-04-05 17:01:29

你得到的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); 
+0

为什么我不能将数组的索引作为BigInteger? – 2015-04-05 17:26:05

+0

是从Biginteger到Integer的类型转换吗? – 2015-04-05 17:31:15

+0

@ASHISHJAISWAL你可以使用'intValue();'方法 – Prashant 2015-04-05 17:49:53