Java 矩阵算术运算程序

编写一个 Java 程序来执行矩阵的算术运算,并附带示例。或者编写一个 Java 程序来执行矩阵或多维数组的加法、减法、乘法、除法和模运算。

在此矩阵算术运算示例中,我们声明了两个矩阵。接下来,我们使用 For 循环来迭代矩阵项。我们在该循环中对 x 和 y 矩阵执行矩阵算术运算,并将结果打印为输出。

public class MatrixArithmeticOperations {

	public static void main(String[] args) {
		int[][] x = {{15, 25, 35}, {45, 55, 65}, {75, 85, 95}};
		int[][] y = {{ 2, 4, 5}, {6, 8, 5}, {10, 15, 20}};
		
		int[][] sum = new int[3][3];
		int i, j;
		
		System.out.println("\nAdd\tSub\tMul\tDiv\tMod");
		
		for(i = 0; i < x.length; i++)
		{
			for(j = 0; j < x[0].length; j++)
			{
				System.out.format("%d \t", (x[i][j] + y[i][j]));
				System.out.format("%d \t", (x[i][j] - y[i][j]));
				System.out.format("%d \t", (x[i][j] * y[i][j]));
				System.out.format("%d \t", (x[i][j] / y[i][j]));
				System.out.format("%d \t", (x[i][j] % y[i][j]));
				System.out.println("");
			}
			System.out.println("");
		}
	}
}
Java program to perform Arithmetic operations on Matrix

Java 矩阵算术运算示例 2

Java 矩阵算术运算逻辑代码与上方相同。但是,此 Java 代码允许用户输入矩阵的行数和列数。接下来,它读取用户输入的两个矩阵,然后执行算术运算。请参阅 C 语言多维数组算术运算程序 来逐个迭代地理解算术运算符的代码分析。

import java.util.Scanner;

public class MatrixArithmeticOperations {
	private static Scanner sc;
	public static void main(String[] args) {
		int i, j, rows, columns;
		
		sc= new Scanner(System.in);
		
		System.out.println("\n Please Enter Matrix Rows and Columns :  ");
		rows = sc.nextInt();
		columns = sc.nextInt();
		
		int[][] arr1 = new int[rows][columns];
		int[][] arr2 = new int[rows][columns];
		int[][] add = new int[rows][columns];
		int[][] sub = new int[rows][columns];
		int[][] mul = new int[rows][columns];
		int[][] div = new int[rows][columns];
		int[][] mod = new int[rows][columns];
		
		System.out.println("\n Please Enter the First Matrix Items :  ");
		for(i = 0; i < rows; i++) {
			for(j = 0; j < columns; j++) {
				arr1[i][j] = sc.nextInt();
			}		
		}
		System.out.println("\n Please Enter the Second Matrix Items :  ");
		for(i = 0; i < rows; i++) {
			for(j = 0; j < columns; j++) {
				arr2[i][j] = sc.nextInt();
			}		
		}
		for(i = 0; i < rows; i++) {
			for(j = 0; j < columns; j++) {
				add[i][j] = arr1[i][j] + arr2[i][j];
				sub[i][j] = arr1[i][j] - arr2[i][j];
				mul[i][j] = arr1[i][j] * arr2[i][j];
				div[i][j] = arr1[i][j] / arr2[i][j];
				mod[i][j] = arr1[i][j] % arr2[i][j];
			}		
		}
		
		System.out.println("\nAdd\t Sub\t Mul\t Div\t Mod");
		for(i = 0; i < rows; i++) {
			for(j = 0; j < columns; j++) {
				System.out.format("%d \t", add[i][j]);
				System.out.format("%d \t", sub[i][j]);
				System.out.format("%d \t", mul[i][j]);
				System.out.format("%d \t", div[i][j]);
				System.out.format("%d \t", mod[i][j]);
				System.out.println("");
			}
			System.out.println(" ");
		}
	}
}

矩阵算术运算输出

 Please Enter Matrix Rows and Columns :  
3 3

 Please Enter the First Matrix Items :  
10 20 30
40 50 60
70 80 90

 Please Enter the Second Matrix Items :  
2 4 6
8 9 5
3 2 1

Add	 Sub	 Mul	 Div	 Mod
12 	8 	20 	5 	0 	
24 	16 	80 	5 	0 	
36 	24 	180 	5 	0 	
 
48 	32 	320 	5 	0 	
59 	41 	450 	5 	5 	
65 	55 	300 	12 	0 	
 
73 	67 	210 	23 	1 	
82 	78 	160 	40 	0 	
91 	89 	90 	90 	0