2013-04-09 100 views
-3

我尝试使用的BigInteger这样的(其中m和n为整数):的BigInteger在Java中

m.substract(BigInteger.ONE), n.substract(BigInteger.ONE) 

它说:

我是什么 “的基本类型int不能调用减法(BigInteger的)”我在这里做错了吗?

+6

试图调用'减法(BigInteger的)'的基本类型'int'。 – 2013-04-09 13:07:42

+0

int是一个原始类型。你必须把你的值包装到Integer类 – ITroubs 2013-04-09 13:07:47

回答

7

int是一个本地数据类型,它是而不是一个对象!

也许你应该声明mnBigIntegers而不是?

+0

它说方法减(BigInteger)是未定义整型 – user2036340 2013-04-09 13:12:35

+0

这是因为它只在类BigInteger中定义,所以m需要是BigInteger类型 – GameDroids 2013-04-09 13:15:20

+0

'int's不是对象,所以它们根本没有方法。在某些情况下,java会将'int'自动装箱到'Integer'。 “整数”不具有(或需要)减法方法。当减去'int's时,使用'-'运算符,例如'm-n'。 '-'运算符仅适用于本机数据类型。当减去'BigInteger's时,你可以使用'subtract'方法,它只适用于其他'BigInteger's。 – 2013-04-09 13:34:48

4

m.substract(BigInteger.ONE)这里m只是一个int它既不是一个BigInteger也不是一个Object任何种类的,但一个原始的。如果你想调用一个方法(substract(BigInteger i)),那么m和n需要是classObject,实际上它的方法是substract(BigInteger i)

你可以做这样的:

BigInteger mBig = new BigInteger(m); // in this case n is a String 
mBig = mBig.subtract(BigInteger.ONE); 

BTW:它被称为和(不带S)

+0

'BigInteger'是不可变的,所以第二行不做任何事情,你需要重新赋值'mBig'。 “减量”的好处。 – 2013-04-09 13:16:15

+0

@ bmorris591谢谢:) – GameDroids 2013-04-09 13:19:40

0

int s为原始时代,他们没有方法不。减去。
盒装类型Integer也没有subract(BigInteger)方法。

你需要要么使int s转换BigInteger s的BigInteger.valueOfintValue使BigInteger s转换int秒。

后一种方法是不安全的,因为BigInteger可能大于Integer.MAX_VALUE

所以,你需要做的

BigInteger.valueOf(m).subtract(BigInteger.ONE), 
BigInteger.valueOf(n).subtract(BigInteger.ONE) 

但是,这是一个有点混乱,为什么不这样做

BigInteger.valueOf(m - 1), BigInteger.valueOf(n - 1)