我尝试使用的BigInteger这样的(其中m和n为整数):的BigInteger在Java中
m.substract(BigInteger.ONE), n.substract(BigInteger.ONE)
它说:
我是什么 “的基本类型int不能调用减法(BigInteger的)”我在这里做错了吗?
我尝试使用的BigInteger这样的(其中m和n为整数):的BigInteger在Java中
m.substract(BigInteger.ONE), n.substract(BigInteger.ONE)
它说:
我是什么 “的基本类型int不能调用减法(BigInteger的)”我在这里做错了吗?
int
是一个本地数据类型,它是而不是一个对象!
也许你应该声明m
和n
为BigIntegers
而不是?
它说方法减(BigInteger)是未定义整型 – user2036340 2013-04-09 13:12:35
这是因为它只在类BigInteger中定义,所以m需要是BigInteger类型 – GameDroids 2013-04-09 13:15:20
'int's不是对象,所以它们根本没有方法。在某些情况下,java会将'int'自动装箱到'Integer'。 “整数”不具有(或需要)减法方法。当减去'int's时,使用'-'运算符,例如'm-n'。 '-'运算符仅适用于本机数据类型。当减去'BigInteger's时,你可以使用'subtract'方法,它只适用于其他'BigInteger's。 – 2013-04-09 13:34:48
m.substract(BigInteger.ONE)这里m只是一个int
它既不是一个BigInteger
也不是一个Object
任何种类的,但一个原始的。如果你想调用一个方法(substract(BigInteger i)
),那么m和n需要是class
的Object
,实际上它的方法是substract(BigInteger i)
。
你可以做这样的:
BigInteger mBig = new BigInteger(m); // in this case n is a String
mBig = mBig.subtract(BigInteger.ONE);
BTW:它被称为减和(不带S)
'BigInteger'是不可变的,所以第二行不做任何事情,你需要重新赋值'mBig'。 “减量”的好处。 – 2013-04-09 13:16:15
@ bmorris591谢谢:) – GameDroids 2013-04-09 13:19:40
int
s为原始时代,他们没有方法不。减去。
盒装类型Integer
也没有subract(BigInteger)
方法。
你需要要么使int
s转换BigInteger
s的BigInteger.valueOf
或intValue
使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)
试图调用'减法(BigInteger的)'的基本类型'int'。 – 2013-04-09 13:07:42
int是一个原始类型。你必须把你的值包装到Integer类 – ITroubs 2013-04-09 13:07:47