2013-08-27 5 views
-1

두 개의 간단한 java 파일에 몇 가지 BigInteger 오브젝트가 있습니다. 그러나, 그것들은 원시 타입이 아니기 때문에, 산술 연산자는 그것들에서 작동하지 않을 것입니다. 이 연산자를 사용 할 때마다 오류를 그래서 같은오브젝트에 대한 산술 연산 방법

: 여기

.\_Mathematics\Formulas\Factorial.java:10: error: bad operand types for binary o 
perator '*' 
         result *= i; 
          ^
    first type: BigInteger 
    second type: int 

그들은 다음과 같습니다

package _Mathematics.Formulas; 
import java.math.*; 

public class Factorial<T extends Number> { 
    public T o; 
    public BigInteger r; 
    public Factorial(int num) { 
     BigInteger result = new BigInteger("1"); 
     for(int i = num; i > 0; i--) 
      result *= i; 
     this.o = num; 
     this.r = result; 
    } 
} 

package _Mathematics.Formulas; 
import java.math.*; 

public class Power<T extends Number> { 
    public T o; 
    public BigInteger r; 
    public Power(T num, int pow) { 
     BigInteger result = new BigInteger(1); 
     for(int i = 0; i < pow; i++) { 
      result *= num; 
     } 
     this.o = num; 
     this.r = result; 
    } 
} 

내가 잠시 동안 주위를 둘러 보았다 이 문제를 해결하는 방법에 대해서는 답변을 찾을 수 없습니다.

누군가가이 문제를 해결할 수 있습니까?

감사합니다.

+11

BigInteger javadoc을 보셨습니까? – sanbhat

답변

10

BigInteger에는 이에 대한 연산자 메소드가 있습니다. BigInteger 자체는 불변이므로 값이

result = result.multiply(num); 
1

당신은 BigInteger 클래스 내부의 곱셈 방법을 사용해야 될 것이다

result *= num; 

다음 결과 예를 들어

에 할당해야합니다.

3

BigInteger에는 산술 연산에 대해 정의 된 고유 한 메소드가 있습니다. 따라서,

result *= num; 

result = result.multiply(num); 

등을해야 마찬가지로 당신이 add, divide, 또는 subtract하십시오.