[英]String of number add together from an array java
I have a project to take a string of an unset amount of numbers together.我有一个项目将一串未设定数量的数字组合在一起。 In the project you have to convert the string to an array of numbers.在项目中,您必须将字符串转换为数字数组。 Then start at the end of the array and add them together.然后从数组的末尾开始并将它们加在一起。 So I guess I am asking is the best way to go about this.所以我想我问的是解决这个问题的最好方法。 My teacher said use divisor and modulus to get the carry over, but that is all he really gave us I have the Int array set set up just cannot get the result to add up right.我的老师说使用除数和模数来得到结转,但这就是他真正给我们的我设置的 Int 数组只是无法得到正确的结果。
public class FunctionLibrary { public static String performAddition(String sNumber1, String sNumber2) { int length = 0;公共类FunctionLibrary { public static String performAddition(String sNumber1, String sNumber2) { int length = 0;
if (sNumber1.length() > sNumber2.length())
{
length = sNumber1.length()-1;
}
else
{
length = sNumber2.length()-1;
}
int[] input1 = new int[length];
int[] input2 = new int[length];
int[] result = new int[length+1];
String resultS = "";
for (int i = length; i > 0; i--)
{
input1[i] = Character.getNumericValue(sNumber1.charAt(i));
}
for (int i = length; i > 0; i--)
{
input2[i] = Character.getNumericValue(sNumber2.charAt(i));
}
for (int i = length; i > 0; i--)
{
int temp = 0;
int divid = 0;
int modulas = 0;
int answer = 0;
temp = input1[i] + input2[i];
divid = temp/10;
modulas = temp%10;
input1[i+1] += modulas;
answer = divid;
result[i] = answer;
}
for (int i = 0; i <= length; i++)
{
resultS += Integer.toString(result[i]);
}
return resultS;
} } } }
No errors as of this edit but no result coming back.此编辑没有错误,但没有返回结果。
This is what I have so far but it just does not want to work right.这是我到目前为止所拥有的,但它只是不想正常工作。 Any help would be appreciated.任何帮助,将不胜感激。
for (int i = length; i >= 0; i--) { for (int i = 长度;i >= 0;i--) {
int temp = 0; int divid = 0; int modulas = 0; int answer = 0; temp = input1[i] + input2[i]; divid = temp/10; modulas = temp%10;
First of all, why not merge lines?首先,为什么不合并线? Second of all, what has division to do with addition?其次,除法与加法有什么关系?
Because you have to carry numbers to the next;因为你必须把数字带到下一个; you will need a variable that's outside the for loop.您将需要一个在 for 循环之外的变量。 Since you add two single-digit numbers, the highest you can get is 18, so you either;由于您将两个个位数相加,您可以得到的最高数字是 18,因此您可以;
get a single-digit you give back, or;得到一个你回馈的一位数,或者;
get 10 + a single-digit you give back得到 10 + 一个你回馈的个位数
So inside the for-loop;所以在for循环里面; you test if the temp is single digit or not:您测试温度是否为个位数:
boolean carryOne = false;布尔carryOne = false;
for (int i = length; i >= 0; i--) { for (int i = 长度;i >= 0;i--) {
int answer = 0; int temp = input1[i] + input2[i]; if(carryOne) { temp++; carryOne = false; } if(temp >= 10) { // two digits: answer = (temp - 10); // you give the left digit back, carryOne = true; // and carry the ten } else answer = temp; // single digit
The biggest process of learning to code, is getting the mindset学习编码的最大过程,是获得心态
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.