0

嘿,我正在尝试将二维数组相乘,但我很困惑。我有这个代码,但它似乎不起作用..

 class MatrixMultiply {
    public static void main(String[] args) {
        int array[][] = { {1,2,-2,0}, {-3,4,7,2}, {6,0,3,1} };
        int array1[][] = { {-1,3}, {0,9}, {1,-11}, {4,-5} };
        int array2[][] = new int[2][3];
        int x= array.length;     
        int y= array1.length;

        for(int i = 0; i < x; i++) {
            for(int j = 0; j < y-1; j++) {
                for(int k = 0; k < y; k++){
                    array2[i][j] += array[i][k]*array1[k][j];
                }
            }  
        }

        System.out.println("After we multiply the two arrays: ");

        for(int i = 0; i < x; i++) {
            for(int j = 0; j < y-1; j++) {
                System.out.print(" "+array2[i][j]);
            }  

            System.out.println();
        }
    }
}
4

1 回答 1

-1

修改使数组相乘的循环。

for (int i = 0; i < x-1; i++) {
  for (int j = 0; j < array1[0].length; j++) {
    for (int k = 0; k < array[0].length; k++) { 
      array2[i][j] += array[i][k] * array1[k][j];
    }
  }
}
于 2013-01-31T05:17:23.927 回答