基于Java菜单的矩阵运算程序


Java中的数组被称为非基本数据类型,它存储固定数量的相同类型的值。它被称为一维数组。而矩阵指的是矩形数组或二维数组。

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

举几个例子:

示例1

Suppose we have inserted two different matrices of 2 rows and 3 columns. Then we will
perform matrix addition and print the result. Let the matrix be:
First Matrix:
2 3 5
9 8 7
Second Matrix:
6 4 8
9 5 4
Matrix after addition:
8 7 13
18 13 11

示例2

Suppose we have inserted two different matrices of 2 rows and 3 columns. Then we will
perform matrix subtraction and print the result. Let the matrix be:
First Matrix:
2 3 5
9 8 7
Second Matrix:
6 4 8
9 5 4
Matrix after Subtraction:
-4 -1 -3
0 3 3

示例3

Suppose we have inserted two different matrices of 2 rows and 3 columns. Then we will
perform matrix multiplication and print the result. Let the matrix be:
First Matrix:
2 3 5
9 8 7
Second Matrix:
6 4 8
9 5 4
Matrix after Multiplication:
12 12 40
81 40 28

语法

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

以下是“for循环”的语法:

for (statement 1; statement 2; statement 3) {
   // code block to be executed
}

算法

步骤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中执行矩阵运算。

更新于:2022年12月27日

3K+ 次浏览

开启你的职业生涯

完成课程获得认证

开始学习
广告