据我所知,这个程序是正确完成的。但是,鉴于例外它似乎没有。我要制作2个长度为x的数组(用户输入),用户输入元素。完成。接下来,将每个元素乘以另一个数组中的相应元素,并添加总和。
Ex,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);
}
答案 0 :(得分:1)
访问de array时,将<=
符号更改为<
。例如:
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]);
}
答案 1 :(得分:0)
elements <= firstArrayLength.length
==&gt; elements < firstArrayLength.length
arrayElements = keyboard.nextInt();
==&gt;&gt; firstArrayLength[elements] = keyboard.nextInt();
secondArrayElements = keyboard.nextInt();
==&gt;&gt; secondArrayLength[elements] = keyboard.nextInt();