一、思路
函数
1.定义两个矩阵数组,先判断矩阵a与b是否可以相乘
2.若不可以,输出矩阵不能相乘,若可以,定义乘积后的矩阵数组为c,使c=a*b,遍历输出c
3.其中定义c为 c[ a的行数 ][ b的列数 ],遍历a,b使他们乘积
主函数
1.编写输入语句
2.分别输入a与b的行数与列数,构造数组a,b,再输入矩阵a,b
3.调用函数

二、Java程序展示

import java.util.Scanner;
public class juzhenchengji {

	public static void chengji(int a[][],int b[][]) {
		
		if(a[0].length != b.length){                    //判断a的列数与b的行数是否相等
			System.out.println("矩阵无法相乘,数组不存在");
		}
		else {
			int x=a.length;
        	int y=b[0].length;
        	int c[][]=new int[x][y];                  //定义乘积后的数组c[ a的行数 ][ b的列数 ]
        	for(int i=0;i<x;i++)
        	{
        		for(int j=0;j<y;j++) 
        		{
        			for (int k = 0; k < b.length; k++)
        				c[i][j] += a[i][k] * b[k][j];     //即c[i][j] = a[0][0]*b[0][0] + a[0][1]*b[1][0] + a[0][2]*b[2][0] + ......
        		}
        	}
        	System.out.println("矩阵相乘后结果为");
    	    for(int m = 0;m<a.length;m++)
    		{
    			for(int n = 0;n<b[0].length;n++)
    			{
    				System.out.print(c[m][n]+"\t");        //"\t"意思是补空格到当前字符串长度到8的整数倍
    			}
    			System.out.println();
    		}	
		}
	}
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.print("请输入第一个矩阵的行数和列数:");
		int x1 = sc.nextInt();
		int y1 = sc.nextInt();
		int[][] arr1 = new int[x1][y1];
		System.out.println("请输入第一个矩阵");
		for (int i = 0; i < x1; i++) {
			for (int j = 0; j <y1; j++) 
				arr1[i][j]=sc.nextInt();
		} 
	   
		System.out.print("请输入第二个矩阵的行数和列数:");
 		int x2 = sc.nextInt();
 		int y2 = sc.nextInt();
 		int[][] arr2 = new int[x2][y2];
 		System.out.println("请输入第二个矩阵");
 		for (int i=0; i<x2; i++) {
 			for (int j = 0; j <y2; j++)
 				arr2[i][j]=sc.nextInt();
 		}
		chengji(arr1,arr2);
	}
}

javascript 矩阵运算 java矩阵乘法函数_javascript矩阵运算