2015-04-05 139 views
1

我想運行這段代碼,但我得到錯誤,我無法解決。請幫忙。不兼容的類型: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

回答

0

你得到的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

相關問題