int SIZE = 512;
p = new BigInteger(SIZE, 15, new Random());
q = new BigInteger(SIZE, 15, new Random());
r = new BigInteger(SIZE, 15, new Random());
n = p.multiply(q);
temp1=n;
n = n.multiply(r);
if (temp1.multiply(r)!=n) System.out.println("test");我的代码在不应该打印test的时候打印出来。为什么?
发布于 2014-04-01 08:20:32
您必须使用equals来比较对象相等。
!=或==比较引用。
BigInteger b0 = new BigInteger("0");
BigInteger b1 = new BigInteger("0");
System.out.println(b0 != b1);
System.out.println(!b0.equals(b1));输出
true
false发布于 2014-04-01 08:21:15
根据这一执行情况:
public BigInteger multiply(BigInteger val) {
if (val.signum == 0 || signum == 0)
return ZERO;
int[] result = multiplyToLen(mag, mag.length,
val.mag, val.mag.length, null);
result = trustedStripLeadingZeroInts(result);
return new BigInteger(result, signum == val.signum ? 1 : -1);
}temp1.multiply(r)返回一个新的 BigInteger对象,它将具有与n不同的地址。使用!temp1.multiply(r).equals(n)。
https://stackoverflow.com/questions/22780293
复制相似问题