Java 菜单驱动程序执行矩阵操作

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 (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中执行矩阵操作。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程