将两个数组的相应元素添加到第三个数组

时间:2015-05-25 08:12:05

标签: java arrays

我试图让用户输入元素arrAarrB应该具有的数量,并且还让用户为两者中的每个对应元素选择他们想要的int号,{{1} }和arrA。然后,使用arrBarrC中相应元素的总和创建第三个arrA,然后打印arrBarrAarrB

输出应如下所示:

arrC

这是我到目前为止编写的代码,我需要帮助我如何使用扫描程序让用户选择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 arrA的输入长度以及{{1}中的元素}和arrB。这就是代码到目前为止的样子: -

arrA

4 个答案:

答案 0 :(得分:1)

假设您只有2个数组可以轻松实现并且不嵌套循环,当您理解这些代码时,您可以使用新循环包装所有方法并创建无限数组,以便根据需要求和。 ..但你必须先了解基础知识:

创建扫描程序并询问用户数组的长度:

Scanner sc = new Scanner(System.in);
// ask user!
System.out.println("Input the length:");
int arrayLength = in.nextInt();

使用给定的长度

创建数组
int[] fistArray = new int[arrayLength];
int[] secondArray = new int[arrayLength];
int[] totals = new int[arrayLength];

填充用户输入的从0到数字的第一个数组迭代位置:

for (int i = 0; i < arrayLength; i ++) {
    System.out.println("Enter a value for first array, position "+ i);
    try {
        firstArray[i] = Integer.parseInt(sc.nextLine());
    } catch (Exception e) {
        System.out.println("Not a valid number!!!);
        i --;
    }
}

填充第二个数组迭代位置,从0到用户输入的数字,得到每个pos的总和:

for (int i = 0; i < in.nextInt(); i ++) {
    System.out.println("Enter a value for second array, position "+ i);
    try {
        secondArray[i] = Integer.parseInt(sc.nextLine());
        totals[i] = fistArray[i] + secondArray[i];
    } catch (Exception e) {
        System.out.println("Not a valid number!!!);
        i --;
    }
}

打印结果:

System.out.println(Arrays.toString(firstArray));          
System.out.println(Arrays.toString(secondArray));       
System.out.println(Arrays.toString(totalsArray));

最后,不要忘记关闭扫描仪以避免内存泄漏,如drgPP所示:

sc.close();

答案 1 :(得分:0)

以下代码应该按您的要求执行:

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

   }


}

System.out.println("sum: " + sum[0]+"," + sum[1] + ","  + sum[2] + ","  + sum[3] );


} }

答案 2 :(得分:0)

public static void main (String[] args){
            Scanner sc = new Scanner(System.in);
            System.out.println("Length of arrays: ");
            try {
                //Initializing length of array
                int length = sc.nextInt();
                //Constructing our arrays based on length
                int[] arrA = new int[length];
                int[] arrB = new int[length];
                int[] arrSum = new int[length];

                //Populating our array A via a loop
                for (int i=0; i<arrA.length; i++) {
                    System.out.println("Values for arrA at index: "+i);
                    int value = sc.nextInt();
                    arrA[i]=value;
                }
                //Populating our array B via a loop
                for (int i=0; i<arrB.length; i++) {
                    System.out.println("Values for arrB at index: "+i);
                    int value = sc.nextInt();
                    arrB[i]=value;
                }
                //Call the method to calcualte our sum which will be in sum array
                arrSum = makeSum(arrA, arrB, length);

                System.out.println(Arrays.toString(arrSum));
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    sc.close();
                }

    }

    // Method to calculate our Sum Array based on the length and the Array A and B
    public static int[] makeSum (int[] arrA, int[] arrB, int length) {
        int[] arrSum = new int[length];
        for (int i=0; i<arrA.length; i++) {
            for (int j=0; j<arrB.length; j++) {
                arrSum[j]=arrA[i]+arrB[j];
            }
        }
        return arrSum;
    }

答案 3 :(得分:-1)

也许这是你的问题:

Scanner scan = new Scanner(System.in);

System.out.println("Enter size: ");
int size =scan.nextInt();

Integer[] arrA =  new Integer[size];
ArrayList<Integer> arrB = new ArrayList<Integer>(size);