0

据我所知,这个程序是正确完成的。但是,鉴于例外情况,它似乎没有。我要制作 2 个长度为 x 的数组(用户输入),用户输入元素。完毕。接下来将每个元素乘以其在另一个数组中的对应元素并添加总和。

例如,array1[0]*array2[0] + array1[1]*array2[1]...

准确的错误是:线程“main”java.lang.ArrayIndexOutOfBoundsException中的异常:

我做了很多不同的循环,下面的最后一个循环是我认为最接近正确但不是正确的。非常感谢您的一些建议,在此先感谢您。

   System.out.println("This program will multiply 2 one dimension arrays of any length. \n The length and contents of the array is entered from the keyboard.");
    System.out.println("Enther the data for the first array. ");

    System.out.println("Enther the length of the array (remember arrays being counting at 0, not 1:");

    int a = 0;
    Scanner keyboard = new Scanner(System.in);
    a = keyboard.nextInt();
    int[] firstArrayLength = new int[a];
    System.out.println("Enter the elements of the first array(remember arrays begin counting at 0, not 1");
    double arrayElements = 0;
    for (int elements = 0; elements <= firstArrayLength.length; elements++) {

        arrayElements = keyboard.nextInt();
    }

    System.out.println("Enter the data for the second array. ");
    System.out.println("Enter the elements of the second  array(remember arrays begin counting at 0, not 1");
    int[] secondArrayLength = new int[a];

    double secondArrayElements = 0;
    for (int elements = 0; elements <= secondArrayLength.length; elements++) {

        secondArrayElements = keyboard.nextInt();
    }
    double [] thirdArray = new double [a];





    for (int i =0; i <=firstArrayLength.length; i++)
       {
        thirdArray[a] = firstArrayLength[i]*secondArrayLength[i];
        }
    System.out.println(thirdArray);
        }
4

2 回答 2

1

<=将符号更改<为访问 de 数组时。例如:

for (int elements = 0; elements < firstArrayLength.length; elements++)
...

请记住,如果length是 4,您可以按以下方式访问元素:

array[0], array[1], array[2], array[3] // 4 elements

array[4]不存在,导致IndexOutOfBounds异常。

编辑

奇怪的输出[I@756a7c99(例如)是因为您将数组打印为:

int a[] = new int[4];
System.out.println(a);

相反,您可能想要打印该数组的元素:

int a[] = new int[4];
for (int i = 0; i < 4; i++) {
    System.out.println(a[i]);
}

编辑 2

public static void main(String[] args) {
    System.out
            .println("This program will multiply 2 one dimension arrays of any length. \n The length and contents of the array is entered from the keyboard.");
    System.out.println("Enther the data for the first array. ");

    System.out
            .println("Enther the length of the array (remember arrays being counting at 0, not 1:");

    int a = 0;
    Scanner keyboard = new Scanner(System.in);
    a = keyboard.nextInt();
    int[] firstArray = new int[a];
    System.out
            .println("Enter the elements of the first array(remember arrays begin counting at 0, not 1");
    for (int elements = 0; elements < firstArray.length; elements++) {

        firstArray[elements] = keyboard.nextInt();
    }

    System.out.println("Enter the data for the second array. ");
    System.out
            .println("Enter the elements of the second  array(remember arrays begin counting at 0, not 1");
    int[] secondArray = new int[a];

    for (int elements = 0; elements < secondArray.length; elements++) {

        secondArray[elements] = keyboard.nextInt();
    }
    double[] thirdArray = new double[a];

    for (int i = 0; i < firstArray.length; i++) {
        thirdArray[i] = firstArray[i]*secondArray[i];
    }

    for (int i = 0; i < thirdArray.length; i++)
        System.out.println(thirdArray[i]);
}
于 2013-11-02T01:07:57.977 回答
0

elements <= firstArrayLength.length==>elements < firstArrayLength.length

arrayElements = keyboard.nextInt();==>>firstArrayLength[elements] = keyboard.nextInt();

secondArrayElements = keyboard.nextInt();==>>secondArrayLength[elements] = keyboard.nextInt();

于 2013-11-02T01:11:54.037 回答