Java 找出每行和每列的乘积

Java 找出每行和每列的乘积

在Java中,数组是一个对象。它是一种非基本数据类型,用于存储相似数据类型的值。在Java中,矩阵就是多维数组,表示多行和多列。

在这里,我们给出了一个包含一组元素的矩阵,并且根据问题要求,我们需要找出每行元素和每列元素的乘积。

在本文中,我们将看到如何使用Java编程语言来实现。

为了给你展示一些示例

示例-1

给定的矩阵=

21  22  23
24  25  26
27  28  29
  • 每行的乘积 −
    • 第一行的乘积为:10626

    • 第二行的乘积为:15600

    • 第三行的乘积为:21924

  • 每列的乘积 −

    • 第一列的乘积为:13608

    • 第二列的乘积为:15400

    • 第三列的乘积为:17342

示例-2

给定矩阵 =

9   2   2   4
1   7   2   6
2   2   4   3
1   4   7   8
  • 每行的乘积 –
    • 第一行的乘积为:144

    • 第二行的乘积为:84

    • 第三行的乘积为:48

    • 第四行的乘积为:224

  • 每列的乘积 –

    • 第一列的乘积为:18

    • 第二列的乘积为:112

    • 第三列的乘积为:112

    • 第四列的乘积为:576

示例-3

给定矩阵 =

1   2   3
4   5   6
7   8   9
  • 每行的乘积 –
    • 该行的乘积为:6

    • 该行的乘积为:120

    • 该行的乘积为:504

  • 每列的乘积 –

    • 第1列的乘积为:28

    • 第2列的乘积为:80

    • 第3列的乘积为:162

步骤

  • 步骤1 - 声明并初始化一个名为inputMatrix的二维矩阵。

  • 步骤2 - 循环遍历矩阵的每一行,并计算每行元素的乘积。然后将每行的乘积打印到控制台。

  • 步骤3 - 对列重复相同的逻辑,并计算每列元素的乘积。

  • 步骤4 - 然后使用不同的消息将每列的乘积打印到控制台。

语法

Java中的Matrix.length方法返回给定矩阵的长度。

下面是其语法的参考 –

inputMatrix.length

其中,‘inputMatrix’是指给定的矩阵。

多个方法

我们提供了不同的方法来解决。

  • 通过静态初始化矩阵元素

  • 通过使用用户自定义方法

让我们逐个查看程序及其输出。

方法1:通过静态初始化矩阵元素

在这个方法中,矩阵元素将在程序中初始化。然后根据算法计算该矩阵的行和列的元素的乘积。

示例

public class Main {
   public static void main(String[] args) {

      //declare and initialized a 2d matrix
      int[][] inputMatrix = {{1, 2, 3}, {1, 2, 3}, {1, 2, 3}};
      System.out.println("Product of each row:");

      //initiate the loop to find the rows product values
      for (int[] r : inputMatrix) {
         int prod = 1;
         for (int value : r) {
            prod *= value;
         }
         System.out.println("The product of the row is: " + prod);
      }
      System.out.println("\nProduct of each column:");

      //initiate the loop to find the columns product values
      for (int a = 0; a < inputMatrix[0].length; a++) {
         int prod = 1;
         for (int b = 0; b < inputMatrix.length; b++) {
            prod *= inputMatrix[b][a];
         }
         System.out.println("The product of the column " + (a + 1) + " is: " + prod);
      }
   }
}

输出

Product of each row:
The product of the row is: 6
The product of the row is: 6
The product of the row is: 6

Product of each column:
The product of the column 1 is: 1
The product of the column 2 is: 8
The product of the column 3 is: 27

方法2:通过使用用户定义的方法

在这种方法中,矩阵元素将在程序中初始化。然后通过将矩阵作为参数传递给用户定义的方法,并在方法内根据算法计算该矩阵的行和列元素的乘积。

示例

public class Main {
   public static void main(String[] args) {
      int[][] inputMatrix = {{11, 22, 33}, {44, 55, 66}, {77, 88, 99}};

      //call the user-defined methods to print the product values
      findRowProduct(inputMatrix);
      findColumnProduct(inputMatrix);
   }

   //user-defined method to find the product of row elements
   private static void findRowProduct(int[][] mat) {
      System.out.println("Product of each row:");
      for (int[] r : mat) {
         int prod = 1;
         for (int value : r) {
            prod *= value;
         }
         System.out.println("The product of the row is: " + prod);
      }
   }

   //user-defined method to find the product of column elements
   private static void findColumnProduct(int[][] mat) {
      System.out.println("\nProduct of each column:");
      for (int a = 0; a < mat[0].length; a++) {
         int prod = 1;
         for (int b = 0; b < mat.length; b++) {
            prod *= mat[b][a];
         }
         System.out.println("The product of the column " + (a + 1) + " is: " + prod);
      }
   }
}

输出

Product of each row:
The product of the row is: 7986
The product of the row is: 159720
The product of the row is: 670824

Product of each column:
The product of the column 1 is: 37268
The product of the column 2 is: 106480
The product of the column 3 is: 215622

在这篇文章中,我们通过使用Java编程语言,探讨了一种逐个查找矩阵每行和每列元素的乘积的不同方法。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程