基於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+ 次瀏覽

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告