據我所知,這個程序是正確完成的。但是,鑑於它似乎沒有例外。我將創建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