public class MatrixMul {
final static int row1 = 4;// 行数
final static int col1 = 5;// 列数
final static int row2 = 5;// 行数
final static int col2 = 4;// 列数
public static void main(String[] args) {
int[][] matrix1 = new int[MatrixMul.row1][MatrixMul.col1];
int[][] matrix2 = new int[MatrixMul.row2][MatrixMul.col2];
for (int i = 0; i < MatrixMul.row1; i++) {
for (int j = 0; j < MatrixMul.col1; j++) {
int value = (int) (Math.random() * 100 + 1);
matrix1[i][j] = value;
}
}
for (int i = 0; i < MatrixMul.row2; i++) {
for (int j = 0; j < MatrixMul.col2; j++) {
int value = (int) (Math.random() * 100 + 1);
matrix2[i][j] = value;
}
}
System.out.println("第一个矩阵是:");
for (int i = 0; i < MatrixMul.row1; i++) {
for (int j = 0; j < MatrixMul.col1; j++) {
System.out.print(matrix1[i][j] + "\t");
}
System.out.println();
}
System.out.println("第二个矩阵是:");
for (int i = 0; i < MatrixMul.row2; i++) {
for (int j = 0; j < MatrixMul.col2; j++) {
System.out.print(matrix2[i][j] + "\t");
}
System.out.println();
}
System.out.println("两个矩阵的乘积是:");
for (int i = 0; i < MatrixMul.row1; i++) {
for(int val : matrix1[i]){
int value = 0;
for(int j=0;j<MatrixMul.col2;j++){
for(int k=0;k<MatrixMul.row2;k++){
value+=val*matrix2[k][j];
}
}
System.out.print(value+"\t");
}
System.out.println();
}
}
}