《java实现矩阵的加-减-乘-转置运算(共3页).docx》由会员分享,可在线阅读,更多相关《java实现矩阵的加-减-乘-转置运算(共3页).docx(3页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
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 = inp
2、ut.nextInt();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 (l
3、ist1.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 stat
5、ic 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 - lis
6、t2ij;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);Syste
7、m.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, list2); / 矩阵相加int
8、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);专心-专注-专业