1、import java.util.*;public class JuZhen / 创建矩阵public static int createJuZhen(int x, int y) Scanner input = new Scanner(System.in);System.out.println(“请输入“ + x + “行“ + y + “列的矩阵数据(行优先): “);int array = new intxy;for (int i = 0; i array.length; i+)for (int j = 0; j arrayi.length; j+) arrayij = input.nex
2、tInt();return array;/ 矩阵输出public static void outputJuZhen(int list) for (int i = 0; i list.length; i+) for (int j = 0; j listi.length; j+) System.out.print(listij + “ “);System.out.println();/ 矩阵求和public static int addJuZhen(int list1, int list2) int list3 = new intlist1.lengthlist10.length;if (list
3、1.length != list2.length | list10.length != list20.length) System.out.println(“行数或列数不同的矩阵不能相加!并将返回 0:!“); else for (int i = 0; i list1.length; i+)for (int j = 0; j list10.length; j+)list3ij = list1ij + list2ij;return list3;/ 矩阵相乘public static int mulJuZhen(int list1, int list2) int list3 = new intli
4、st1.lengthlist20.length;if (list1.length != list20.length | list10.length != list2.length)System.out.println(“两矩阵不能符合条件相乘!并将返回 0:!“);elsefor (int i = 0; i list1.length; i+)for (int j = 0; j list20.length; j+)for (int k = 0; k list20.length; k+)list3ij += list1ik * list2kj;return list3;/ 矩阵相减public s
5、tatic int substractJuZhen(int list1, int list2) int list5 = new intlist1.lengthlist10.length;if (list1.length != list2.length | list10.length != list20.length) System.out.println(“行数或列数不同的矩阵不能相加!并将返回 0:!“); else for (int i = 0; i list1.length; i+)for (int j = 0; j list10.length; j+)list5ij = list1ij
6、 - list2ij;return list5;/ 矩阵转置public static int zhuanzhiJuZhen(int list) int list6 = new intlist.lengthlist0.length;for (int i = 0; i list.length; i+)for (int j = 0; j list0.length; j+)list6ij = listji;return list6;/ Main 函数测试public static void main(String args) Scanner input = new Scanner(System.in
7、);System.out.println(“请输入所要创建第一个矩阵的行数和列数(如:3 3):“);int x = input.nextInt();int y = input.nextInt();int list1 = createJuZhen(x, y);/ 创建矩阵 1System.out.println(“请输入所要创建第 2 个矩阵的行数和列数(如: 3 3):“);x = input.nextInt();y = input.nextInt();int list2 = createJuZhen(x, y);/ 创建矩阵 2int list3 = addJuZhen(list1, li
8、st2); / 矩阵相加int list4 = mulJuZhen(list1, list2); / 矩阵相乘int list5 = substractJuZhen(list1, list2);/ 矩阵相减int list6 = zhuanzhiJuZhen(list1);/ 矩阵转置int list7 = zhuanzhiJuZhen(list2);/ 矩阵转置/ 输出结果System.out.println(“矩阵 1 为:“);outputJuZhen(list1);System.out.println(“矩阵 2 为:“);outputJuZhen(list2);System.out.println(“矩阵 1 与矩阵 2 相加为:“);outputJuZhen(list3);System.out.println(“矩阵 1 与矩阵 2 相减为:“);outputJuZhen(list5);System.out.println(“矩阵 1 与矩阵 2 相乘为:“);outputJuZhen(list4);System.out.println(“矩阵 1 的转置为:“);outputJuZhen(list6);System.out.println(“矩阵 2 的转置为:“);outputJuZhen(list7);