0
所以我必須做一個方法,添加兩個大數字(作爲兩個數組,其中每個數字是不同的字符)。我已經做了一個代碼,但它不能正常工作。有人能幫我看看嗎?添加兩個大數字作爲兩個數組
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;
}
你需要解釋爲什麼代碼不工作?你得到的是什麼? –
請詳細解釋你的代碼有什麼問題。想象你的循環和條件是相當困難的 –
代碼審查,有[CodeReview](http://codereview.stackexchange.com/)... :) – Fildor