简体   繁体   English

从数组java中将数字字符串相加

[英]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.

相关问题 如何从 java 中的字符串数组中获取数字 - How to get number from string array in java 如何使Java获得一个数字和一个新数字并将它们加在一起 - How to make java get a number and a new number and add them together java 字符串数组:从用户那里获取信息并显示数组中的数字 - java String Array : getting information from the user and display the number in the array 将字符串数组中的值相加 - Add the values from an array of strings together 我如何从字符串中提取整数,然后在Java中将它们加在一起而没有split方法/数组? - How would I extract integers from a string and add them together in Java without the split method/arrays? Java在制表符中将文本从字符串数组分隔为字符串矢量 - Java Add Text in Tab Delimited From String Array To String Vector 如何在Java中从数组添加或删除随机数? - How to add or remove a random number from an Array in Java? 使用嵌套For循环“添加”具有数值的两个字符串数组 - Using a Nested For Loop to “Add” Two String Arrays With Number Values Together 如何将具有不同位数的整数的数组表示形式相加? - How to add together an array representation of an integer with different number of digits? 如何在 Android/Java 中将字符串和数字后缀连接在一起? - How to join string and number suffixes together in Android/Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM