-1

我的代码需要将数组的每一行乘以下一行的每一列。用户输入高度和宽度,程序将随机生成数组值。如果数组相等,例如 (3x3) (3x3) 或者即使您这样做 (3x2) (2x3),它也不会给出任何错误。但是,如果您输入类似 (3x3)(3x2) 的内容,则会出现越界异常。

public class Multiply {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        int width = -1;
        int height = 0;
        int width2 = 0;
        int height2 = 0;

        while (width != height2) {

            while (height <= 0) {
                System.out.print("Enter a height for array1: ");
                height = checkInt(scan);
            }

            while (width <= 0) {
                System.out.print("Enter a width for array1: ");
                width = checkInt(scan);
            }

            while (height2 <= 0) {
                System.out.print("Enter a height for array2: ");
                height2 = checkInt(scan);
            }

            while (width2 <= 0) {
                System.out.print("Enter a width for array2: ");
                width2 = checkInt(scan);
            }

            if (width != height2) {
                System.out
                        .println("Error! Dimensions of matrices not compatible. Try again.");
                width = -1;
                height = 0;
                width2 = 0;
                height2 = 0;
            }
        }

        int array1[][] = randomMatrix(height, width);
        int array2[][] = randomMatrix(height2, width2);
        int product[][] = matrixMultiply(array1, array2, height, width2);

        printArray(product);

    }// end of main method

    public static int[][] randomMatrix(int height, int width) {
        int array1[][] = new int[height][width];
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                array1[i][j] = (int) (-10 + Math.random() * 20);
            }
        }
        return array1;
    }// end of randomMatrix method

    public static int[][] matrixMultiply(int[][] array1, int[][] array2, int height, int width) {
        int[][] product = new int[height][width];
        for (int i = 0; i < array1.length; i++) {
            for (int j = 0; j < array1[i].length; j++) {
                int prod = 0;
                for (int k = 0; k < array2.length; k++) {
                    prod = prod + array1[i][k] * array2[k][j];
                }
                product[i][j] = prod;
            }
        }
        return product;
    }// end of matrixMultiply method

    public static void printArray(int[][] array) {
        System.out.println(" ");
        System.out.println("Row Major Representation:");

        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                System.out.print(array[i][j] + " ");
            }
            System.out.println();
        }
        System.out.println(" ");
        System.out.println("Column Major Representation:");

        for (int i = 0; i < array[1].length; i++) {
            for (int j = 0; j < array.length; j++) {
                System.out.print(array[j][i] + " ");
            }

            System.out.println();
        }
    }

    public static int checkInt(Scanner scan) {
        int width = 0;
        if (scan.hasNextInt()) {
            width = scan.nextInt();
            return width;
        } else {
            scan.next();
            return 0;
        }

    }

}// end of public class Multiply
4

1 回答 1

0

变量“j”(第 82 行)在其最终迭代中的值导致了越界异常。(3x3)(3x2) 发生错误,因为在最后的“j”迭代中,j 等于 2,使得该值大于 1(j 位置的最高索引值)。添加一些逻辑来检查何时 j > 最高索引将防止该问题。

于 2015-04-21T19:35:12.097 回答