我试图用.add方法做一个自定义BigInteger类。 这是我的代码:
public BigNumber plus(BigNumber other) {
int[] resultPointer = new int[Math.max(digits.length, other.digits.length) + 1];
int carry = 0;
for (int i = 0; i < resultPointer.length - 1; i++) {
int left = digits.length > i ? digits[i] : 0;
int right = other.digits.length > i ? other.digits[i] : 0;
int result = left + right + carry;
carry = result >= 10 ? result - 9 : 0;
resultPointer[i] = result <= 9 ? result : 9;
}
return new BigNumber(resultPointer);
}
并得出它的BigNumber(我的biginteger名称类为50,具体为int {5,0})