0
我正在構建具有大整數的對象並將它們添加在一起。問題在於,我不知道如何正確編碼。我構造一個值爲123456789的對象,然後通過深度複製方法構造第二個對象,其值爲123456789。我必須遍歷並將最低有效位數字中的每個數字添加到最高有效數字。這是我的代碼在邏輯上給我。
我已經創建了一個bigNumber 123456789,我必須將其複製完成。然後,我必須添加兩個bigNumber的一起添加值大數字
This is my main method
public static void main(String[] args) {
BigNumber b1 = new BigNumber();
for (int i=9; i>=1; i--)
{
b1.addDigit(i);
}
System.out.println("b1 is " + b1.toString()); //return 123456789; corrent
BigNumber b2 = new BigNumber(b1); //Creates a deep copy of b1 123456789
System.out.println("b2: " + b2.toString()); //return 123456789
System.out.println(b1.add(b2).toString()); //should return 123456789 + 123456789 = 246913578
}
這是我的add方法
public BigNumber add(BigNumber otherNum)
{
BigNumber newNum = new BigNumber(); // Create the new Number
BigNumber secNum = new BigNumber(); //
DigitNode temp = head;
while (temp != null) { //while loop to set the numbers 123456789
secNum.addDigit(temp.getDigit());
temp = temp.next;
}
//At this I'm lost. I need to somehow get b1.. then get add it to my secNum that I made to 123456789, and put that into newNum and return it..
}
這是事物的總體思路? – user3011391
我明白了。謝謝! – user3011391