据我所知,这个程序是正确完成的。但是,鉴于例外情况,它似乎没有。我要制作 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);
}