我正在进行一项任务,我需要使用大数字进行计算而不使用bignumber类.
我现在理解我将如何添加2个数字的理论(我把它们作为字符串,然后插入到int数组中)我将添加最后的数字并取任何超过9的数字并将其添加到下一个数字.
我已经提供了这种方法来使用:
LargeInteger sum = firstInt.add(secondInt);
Run Code Online (Sandbox Code Playgroud)
我对如何制作这个方法感到有点困惑,因为它只需要参数中的2个数字中的1个.
这是我到目前为止所做的其他相关代码:
主要:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String string1;
String string2;
int exp =0;
System.out.print("Enter the first integer: ");
//Store up the input string “string1” entered by the user from the keyboard.
string1 = input.next();
LargeInteger firstInt = new LargeInteger(string1);
System.out.print("Enter the second integer: ");
string2 = input.next();
//Store up the input string “string2” entered by the user from the keyboard.
LargeInteger secondInt = new LargeInteger(string2);
LargeInteger sum = firstInt.add(secondInt);
System.out.printf (" Sum = %s \n", sum.display());
}
Run Code Online (Sandbox Code Playgroud)
这是我的第二个类LargeInteger(还没有添加方法):
public class LargeInteger {
private int[] intArray;
//convert the strings to array
public LargeInteger(String s) {
intArray = new int[s.length()];
for (int i = 0; i < s.length(); i++) {
intArray[i] = Character.digit(s.charAt(i), 10); // in base 10
}
}
//display the strings
public String display() {
String result="";
for (int i = 0; i < intArray.length; i++) {
result += intArray[i];
}
return result.toString();
}
}
Run Code Online (Sandbox Code Playgroud)
LargeInteger sum = firstInt.add(secondInt);
Run Code Online (Sandbox Code Playgroud)
这意味着:请firstInt你能给secondInt自己增加的结果吗?
| 归档时间: |
|
| 查看次数: |
1167 次 |
| 最近记录: |