Java 菜单驱动程序执行矩阵运算

javaserver side programmingprogramming

Java 中的数组是一种非原始数据类型,可存储固定数量的单一类型值。它被称为一维数组。而矩阵是指矩形数组或二维数组。

在本文中,我们将了解如何使用 Java 菜单驱动程序执行不同的矩阵运算,如加法、减法和乘法。我们将使用 switch case 实现应用程序。

向您展示一些实例 

实例 1

假设我们插入了两个不同的 2 行 3 列矩阵。然后我们将
执行矩阵加法并打印结果。假设矩阵为:
第一个矩阵:
2 3 5
9 8 7
第二个矩阵:
6 4 8
9 5 4
相加后的矩阵:
8 7 13
18 13 11

实例-2

假设我们插入了两个不同的 2 行 3 列矩阵。然后我们将执行矩阵减法并打印结果。假设矩阵为:
第一个矩阵:
2 3 5
9 8 7
第二个矩阵:
6 4 8
9 5 4
相减后的矩阵:
-4 -1 -3
0 3 3

实例-3

假设我们插入了两个不同的 2 行 3 列矩阵。然后我们将执行矩阵乘法并打印结果。让矩阵为:
第一个矩阵:
2 3 5
9 8 7
第二个矩阵:
6 4 8
9 5 4
乘法后的矩阵:
12 12 40
81 40 28

语法

为了计算矩阵加法、减法和乘法,我们使用带有一些基本逻辑的 for 循环。

以下是"for 循环"的语法 -

for (statement 1; statement 2; statement 3) {
// 要执行的代码块
}

算法

步骤 1 - 要求用户输入两个矩阵。

步骤 2 - 显示菜单。

步骤 3 - 要求用户输入他们的选择。

步骤 4 - 使用 switch case 转到选择并执行操作。

步骤 5 - 打印结果。

让我们看一下程序以清楚地理解它。

示例

import java.util.*; public class Main{ public static void main(String args[]){ Scanner s = new Scanner( System.in ); int p, q, m, n; System.out.print("Enter number of rows in first matrix: "); p = s.nextInt(); System.out.print("Enter number of columns in first matrix: "); q = s.nextInt(); System.out.print("Enter number of rows in second matrix: "); m = s.nextInt(); System.out.print("Enter number of columns in second matrix: "); n = s.nextInt(); int a[][] = new int[p][q]; int b[][] = new int[m][n]; int c[][] = new int[m][n]; System.out.println("Enter all the elements of first matrix:"); for (int i = 0; i < p; i++) { for (int j = 0; j < q; j++) { a[i][j] = s.nextInt(); } } System.out.println("Enter all the elements of second matrix:"); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { b[i][j] = s.nextInt(); } } System.out.println("First Matrix:"); for (int i = 0; i < p; i++) { for (int j = 0; j < q; j++) { System.out.print(a[i][j]+" "); } System.out.println(""); } System.out.println("Second Matrix:"); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { System.out.print(b[i][j]+" "); } System.out.println(""); } mainLoop: while (true) { System.out.println("\n***Menu***"); System.out.println("1. Matrix Addition"); System.out.println("2. Matrix Subtraction"); System.out.println("3. Matrix Multiplication"); System.out.println("4. Terminate the program"); System.out.println("Enter action number (1-4): "); int command; if ( s.hasNextInt() ) { command = s.nextInt(); s.nextLine(); } else { System.out.println("\nILLEGAL RESPONSE. YOU MUST ENTER A NUMBER."); s.nextLine(); continue; } switch(command) { case 1: if (p == m &&& q == n){ for (int i = 0; i < p; i++) for (int j = 0; j <n; j++) { for (int k = 0; k < q; k++) { c[i][j] = a[i][j] + b[i][j]; } } } System.out.println("Matrix after addition:"); for (int i = 0; i <p; i++){ for (int j = 0; j < n; j++) { System.out.print(c[i][j]+" "); } System.out.println(""); } } else{ System.out.println("Addition would not be possible"); } break; case 2: if (p == m && q == n){ for (int i = 0; i < p; i++) { for (int j = 0; j < n; j++) { for (int k = 0; k < q; k++) { c[i][j] = a[i][j] - b[i][j]; } } } System.out.println("Matrix after Subtraction:"); for (int i = 0; i < p; i++) { for (int j = 0; j < n; j++) { System.out.print(c[i][j]+" "); } System.out.println(""); } } else{ System.out.println("Subtraction would not be possible"); } break; case 3: if (p == m && q == n){ for (int i = 0; i < p; i++) { for (int j = 0; j < n; j++) { for (int k = 0; k < q; k++) { c[i][j] = a[i][j] * b[i][j]; } } } System.out.println("Matrix after Multiplication:"); for (int i = 0; i < p; i++){ for (int j = 0; j < n; j++) { System.out.print(c[i][j]+" "); } System.out.println(""); } } else{ System.out.println("Multiplication would not be possible"); } break; case 4: System.out.println("Program terminated"); break mainLoop; default: System.out.println("Wrong choice!!"); } } }

输出

Enter number of rows in first matrix: 2
Enter number of columns in first matrix: 2
Enter number of rows in second matrix: 2
Enter number of columns in second matrix: 2
Enter all the elements of first matrix:
1 2 3 4
Enter all the elements of second matrix:
5 6 7 8
First Matrix:
1 2
3 4
Second Matrix:
5 6
7 8

***Menu***
1. Matrix Addition
2. Matrix Subtraction
3. Matrix Multiplication
4. Terminate the program
Enter action number (1-4):
1
Matrix after addition:
6 8
10 12

***Menu***
1. Matrix Addition
2. Matrix Subtraction
3. Matrix Multiplication
4. Terminate the program
Enter action number (1-4):
2
Matrix after Subtraction
:
-4
-4
-4
-4

***Menu***
1. Matrix Addition
2. Matrix Subtraction
3. Matrix Multiplication
4. Terminate the program
Enter action number (1-4):
3
Matrix after Multiplication:
5 12
21 32

***Menu***
1. Matrix Addition
2. Matrix Subtraction
3. Matrix Multiplication
4. Terminate the program
Enter action number (1-4):
4
Program terminated

在本文中,我们探讨了如何使用菜单驱动方法在 Java 中执行矩阵运算。


相关文章