据我所知,这个程序是正确完成的。但是,鉴于它似乎没有例外。我将创建2个长度为x的数组(用户输入),用户将输入这些元素。完成。接下来,将每个元素乘以另一个数组中的相应元素并添加总和。越界异常乘数组
例,ARRAY1 [0] *数组2 [0] + ARRAY1 [1] *数组2 [1] ...
精确的错误是:异常在线程 “主” 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);
}
我知道我的循环不加呢,我没有补充说因为我认为它现在还没有必要。 – user2946846