Java 使用两个不同的整数对象数组添加方法
我试图在一个类中实现一个方法,该方法为被调用对象和其他对象表示的整数之和创建并返回一个新对象 比如说Java 使用两个不同的整数对象数组添加方法,java,Java,我试图在一个类中实现一个方法,该方法为被调用对象和其他对象表示的整数之和创建并返回一个新对象 比如说 BigInt val1 = new BigInt(1111111); BigInt val2 = new BigInt(2222); BigInt sum = val1.add(val2); System.out.println(sum); should print 1113333 我的add方法似乎工作得很好,但当它到达最后一个索引时,它不会将1带到最后一个索引。 这是我的代码和测试用例 p
BigInt val1 = new BigInt(1111111);
BigInt val2 = new BigInt(2222);
BigInt sum = val1.add(val2);
System.out.println(sum);
should print
1113333
我的add方法似乎工作得很好,但当它到达最后一个索引时,它不会将1带到最后一个索引。
这是我的代码和测试用例
public BigInt add(BigInt other) {
// int length = this.digits.length < other.digits.length ? this.digits.length : other.digits.length;
BigInt added = new BigInt();
int MAX_numsig = this.numSigDigits < other.numSigDigits ? other.getNumSigDigits() : this.getNumSigDigits();
added.numSigDigits = MAX_numsig;
if (other == null) {
throw new IllegalArgumentException("parameter is null");
}
if (this.getNumSigDigits() >= SIZE || other.getNumSigDigits() >= SIZE) {
throw new ArithmeticException("Sum size bigger than final static value SIZE");
}
int carry = 0;
for (int i = this.digits.length - 1; i >= 0; i--) {
if (this.digits[i] + other.digits[i] + carry < 10) {
added.digits[i] = this.digits[i] + other.digits[i] + carry;
carry = 0;
}
if (this.digits[i] + other.digits[i] + carry >= 10) {
added.digits[i] = (this.digits[i] + other.digits[i] + carry) % 10;
if (i == SIZE - MAX_numsig) {
added.numSigDigits = MAX_numsig + 1;
}
if (i == 0) {
throw new ArithmeticException("overflow");
} else {
carry = 1;
}
}
}
return added;
}
此测试用例应返回123456789123456789。
相反,它返回的是23456789123456789。我不明白为什么1没有通过最后一个索引
大小是默认的静态最终值20。
有人能帮我解决一个错误吗?原因是两个数字之和的位数可能大于两个数字,例如,在您给定的示例中,和的位数为18,两个值的位数为17。但是您的代码没有考虑这一部分 因此,如果和中的位数较大,则循环存在后将有一个进位值。您应该将进位值添加到结果中
希望能有帮助。如果你不懂任何部分,请告诉我 这里有一种方法。可能有更有效的方法,但它突出了一些需要解决的问题
String a = "9999";
String b = "999";
// pad smallest on left with 0's.
int diff = a.length() - b.length();
if (diff < 0) {
a = "0".repeat(-diff) + a;
} else {
b = "0".repeat(diff) + b;
}
// convert both string to arrays of int digits.
int[] aa = a.chars().map(c -> c - '0').toArray();
int[] bb = b.chars().map(c -> c - '0').toArray();
int carry = 0;
// iterate over the array and add and process the carry bit
for (int i = aa.length-1; i >= 0; i--) {
bb[i] += (aa[i] + carry);
carry = 0;
if (bb[i] > 9) {
bb[i] %= 10;
carry = 1;
}
}
// Now convert the array of digits to a numeric string.
String result =
Arrays.stream(bb).mapToObj(c -> (char)(c + '0') + "")
.collect(Collectors.joining());
// there could still be a carry to process. e.g "999" + "1"
if (carry == 1) {
result = '1' + result;
}
System.out.println(result);
}
String a=“9999”;
字符串b=“999”;
//用0填充左侧的最小值。
int diff=a.length()-b.length();
如果(差异<0){
a=“0”。重复(-diff)+a;
}否则{
b=“0”。重复(差异)+b;
}
//将两个字符串转换为整数数字数组。
int[]aa=a.chars().map(c->c-'0').toArray();
int[]bb=b.chars().map(c->c-'0').toArray();
整数进位=0;
//迭代数组并添加和处理进位
对于(int i=aa.length-1;i>=0;i--){
bb[i]+=(aa[i]+进位);
进位=0;
if(bb[i]>9){
bb[i]=10;
进位=1;
}
}
//现在将数字数组转换为数字字符串。
字符串结果=
Arrays.stream(bb.mapToObj(c->(char)(c++'0')+“”)
.collect(收集器.joining());
//仍然可能有一个结转过程。e、 g“999”+“1”
如果(进位==1){
结果='1'+结果;
}
系统输出打印项次(结果);
}
String a = "9999";
String b = "999";
// pad smallest on left with 0's.
int diff = a.length() - b.length();
if (diff < 0) {
a = "0".repeat(-diff) + a;
} else {
b = "0".repeat(diff) + b;
}
// convert both string to arrays of int digits.
int[] aa = a.chars().map(c -> c - '0').toArray();
int[] bb = b.chars().map(c -> c - '0').toArray();
int carry = 0;
// iterate over the array and add and process the carry bit
for (int i = aa.length-1; i >= 0; i--) {
bb[i] += (aa[i] + carry);
carry = 0;
if (bb[i] > 9) {
bb[i] %= 10;
carry = 1;
}
}
// Now convert the array of digits to a numeric string.
String result =
Arrays.stream(bb).mapToObj(c -> (char)(c + '0') + "")
.collect(Collectors.joining());
// there could still be a carry to process. e.g "999" + "1"
if (carry == 1) {
result = '1' + result;
}
System.out.println(result);
}