简体   繁体   中英

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). Finally, I want my code to add the elements of these two array into Array C. So the output should look like:

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.

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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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