gpt4 book ai didi

java - 将两个大数相加作为两个数组

转载 作者:行者123 更新时间:2023-12-02 06:18:18 25 4
gpt4 key购买 nike

所以我必须创建一个方法来添加两个大数字(制成两个数组,其中每个数字都是不同的字符)。我已经编写了代码,但它无法正常工作。有人可以帮我看看吗?

public BigNumber add(BigNumber number2){
BigNumber x = null;
char[] m = null;
long y = 0;
boolean tmpBool = false;
boolean leftIsLonger = false;

if (this.n.length >= number2.n.length){
m = new char[this.n.length + 1];
y = number2.n.length;
leftIsLonger = true;
}else{
m = new char[this.n.length + 1];
y = this.n.length;
}

int i;
for (i = 0; i < y; i++){
char[] tmp1 = new char[1];
this.number.getChars(i, i, tmp1, 1);
int left = Character.getNumericValue(tmp1[0]);

int j;
for (j = 0; j < y; j++){
char[] tmp2 = new char[1];
this.number.getChars(i, i, tmp2, 1);
int right = Character.getNumericValue(tmp2[0]);

int z = left + right;

if (tmpBool){
z++;
tmpBool = false;
}

if (z > 9){
tmpBool = true;
z = z%10;
}

m[i]= (char) z;
}}

for (int k = i; k < m.length - 1; k--){
if (leftIsLonger){
if (tmpBool){
int c = Character.getNumericValue(this.n[k]);
if (c > 9){
tmpBool = true;
c = c%10;
m[k] = (char) (c);
}else{
tmpBool = false;
m[k] = (char) (c + 1);
}
}else
m[k] = this.n[k];
}else{
if (tmpBool){
int c = Character.getNumericValue(number2.n[k]);
if (c > 9){
tmpBool = true;
c = c%10;
m[k] = (char) (c);
}else{
tmpBool = false;
m[k] = (char) (c + 1);
}
}else
m[k] = this.n[k];
}
}

return x;
}

最佳答案

首先创建BigNumber x = null,最后返回它。但在这中间,我似乎找不到任何设置的地方?所以整个函数“优化”为

public BigNumber add(BigNumber number2){
return null;
}

这就是你所做的,这就是为什么它总是返回 null。

关于java - 将两个大数相加作为两个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21255878/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com