简体   繁体   English

元素的总和到第三个数组

[英]Array sum of elements to third array

I want the code to get the sum of the corresponding elements of two arrays. 我希望代码获取两个数组的相应元素的总和。 I am trying to get to add the elements. 我试图添加元素。 However, I am really confused as how to do this. 但是,我对如何做到这一点感到非常困惑。 I am trying to get this output to match the test. 我试图获得此输出以匹配测试。 The user will input what the length of the array would be and what the elements of each of the array (arrayA) and (arrayB). 用户将输入数组的长度以及数组(arrayA)和(arrayB)的每个元素。 Finally, I want my code to add the elements of these two array into Array C. So the output should look like: 最后,我希望我的代码将这两个数组的元素添加到数组C中。因此输出应如下所示:

Input the length: 5
Enter a value for first array, position 0: 1
Enter a value for first array, position 1: 6
Enter a value for first array, position 2: 13
Enter a value for first array, position 3: -3
Enter a value for first array, position 4: 8
Enter a value for second array, position 0: 9
Enter a value for second array, position 1: -4
Enter a value for second array, position 2: 1
Enter a value for second array, position 3: 65
Enter a value for second array, position 4: 18
first: 1 6 13 -3 8
second: 9 -4 1 65 18
result: 10 2 14 62 26

I have written the code so far, however it only calculates if the array length is 4. Please tell me as to how to make the program calculate the sum of the corresponding elements of two arrays of any length. 到目前为止,我已经编写了代码,但是它仅计算数组长度是否为4。请告诉我如何使程序计算任意长度的两个数组的相应元素之和。

import java.util.*;

class ArrayArithmetic
{
   public static void main ( String[] args )
   {
      Scanner in = new Scanner(System.in);
      System.out.print("Input the length: ");
      int len = in.nextInt();

      int[] arrA   = new int[len];
      int[] arrB   = new int[len];
      int[] sum    = new int[len];
      for (int i = 0; i < len; i++){
        System.out.print("Enter a value for first array, position " + i + ": ");
        arrA[i] = in.nextInt();
    }
    for (int i = 0; i < len; i++){
        System.out.print("Enter a value for second array, position " + i + ": ");
        arrB[i] = in.nextInt();
    }

    for(int i = 0; i < arrA.length; i++)
    {
     for(int j = 0; i < arrB.length; i++)
     {
       sum[i] = arrA[i] + arrB[i];

   }

    for(int i = 0; i < arrA.length; i++)

}

    System.out.println("first: "+Arrays.toString(arrA));          
    System.out.println("second:"+Arrays.toString(arrB));       
    System.out.println("result: " + sum[0]+"," + sum[1] + ","  + sum[2] + ","  + sum[3] );


    } 
}

Replace this segment of code: 替换这段代码:

 for(int i = 0; i < arrA.length; i++)
{
 for(int j = 0; i < arrB.length; i++)
 {
   sum[i] = arrA[i] + arrB[i];
 }

with

 for(int i = 0; i < len; i++)
{
   sum[i] = arrA[i] + arrB[i];
 }

and the last few lines: 最后几行:

for(int i = 0; i < arrA.length; i++)

}

System.out.println("first: "+Arrays.toString(arrA));          
System.out.println("second:"+Arrays.toString(arrB));       
System.out.println("result: " + sum[0]+"," + sum[1] + ","  + sum[2] + ","  + sum[3] );

with

System.out.println("first: "+Arrays.toString(arrA));          
System.out.println("second:"+Arrays.toString(arrB));       
System.out.println("result: " + Arrays.toString(sum));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM