Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 越界异常乘法数组_Java_Arrays - Fatal编程技术网

Java 越界异常乘法数组

Java 越界异常乘法数组,java,arrays,Java,Arrays,就我所知,这个程序是正确的。然而,考虑到例外情况,似乎并非如此。我将制作2个长度为x的数组(用户输入),用户将输入元素。完成。接下来,将每个元素与另一个数组中对应的元素相乘,并将总和相加 例如,array1[0]*array2[0]+array1[1]*array2[1] 精确的错误是:线程“main”java.lang.ArrayIndexOutOfBoundsException中的异常: 我做了很多不同的循环,下面最后一个循环,我用额外的间隔来标识,我认为最接近正确的循环,但不是。我将非常感

就我所知,这个程序是正确的。然而,考虑到例外情况,似乎并非如此。我将制作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);
        }
System.out.println(“此程序将两个任意长度的一维数组相乘。\n数组的长度和内容是从键盘输入的。”);
System.out.println(“插入第一个数组的数据”);
println(“增加数组的长度(记住数组计数为0,而不是1:”);
int a=0;
扫描仪键盘=新扫描仪(System.in);
a=键盘.nextInt();
int[]firstArrayLength=新的int[a];
System.out.println(“输入第一个数组的元素(记住数组从0开始计数,而不是1”);
双数组元素=0;
对于(int elements=0;elements更改
elements
elements

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


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

我知道我的循环还没有添加,我没有添加到它,因为我认为现在还没有必要。好的,这很有帮助,但现在程序打印了“[D@17b0b765"不是一个有效的答案。那是什么?好的,我修正了这个问题,现在我的答案是0.0。如果我是正确的,小数点是因为double,但是零让我感到不舒服。我不是100%确定,但你可能想这样做:
thirdArray[I]=firstArrayLength[I]*secondArrayLength[I];
然后,打印元素。注意我更改了
[a]
[i]
。实际上我在读你的评论之前就试过了,但不幸的是仍然得到了0.0。得到了。非常感谢Christian!我做到了:因为(int i=0;ifor (int elements = 0; elements < firstArrayLength.length; elements++) ...
array[0], array[1], array[2], array[3] // 4 elements
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]);
}
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]);
}