Java 程序查找矩阵行和列的和

编写一个 Java 程序,通过示例查找每个矩阵行和列的和。 或者 Java 程序计算给定矩阵或多维数组中每一行和每一列的和。

在这个矩阵行和列求和的示例中,我们声明了一个具有随机值的 3*3 SumOfRowCols_arr 整型矩阵。 接下来,我们使用 for 循环迭代 SumOfRowCols_arr 矩阵项。 在 for 循环内,我们正在计算 SumOfRowCols_arr 矩阵的行和列的和。

public class SumOfMatrixRowsColumns {
	
	public static void main(String[] args) {
		
		int i, j, row_sum, column_sum;	
	
		int[][] SumOfRowCols_arr = {{11, 21, 31}, {41, 51, 61}, {71, 81, 91}};
	
		
		for(i = 0; i < SumOfRowCols_arr.length; i++)
		{
			row_sum = 0;
			column_sum = 0;
			for(j = 0; j < SumOfRowCols_arr[0].length; j++)
			{
				row_sum = row_sum + SumOfRowCols_arr[i][j];
				column_sum = column_sum + SumOfRowCols_arr[j][i];
			}
			System.out.println("\nThe Sum of Matrix Items "
					+ "in Row " + i + " = " + row_sum);
			System.out.println("\nThe Sum of Matrix Items "
					+ "in Column " + i + " = " + column_sum);
		}
	}
}

矩阵行和列的和输出


The Sum of Matrix Items in Row 0 = 63

The Sum of Matrix Items in Column 0 = 123

The Sum of Matrix Items in Row 1 = 153

The Sum of Matrix Items in Column 1 = 153

The Sum of Matrix Items in Row 2 = 243

The Sum of Matrix Items in Column 2 = 183

Java 程序查找矩阵行和列的和示例 2

这个 Java 矩阵行和列的和代码与上面相同。 但是,我们使用了两个独立的 for 循环来计算行和列的和。 我建议你参考 Java 计算每一列的和Java 计算每一行的和示例。

public class SumOfMatrixRowsColumns {
	
	public static void main(String[] args) {
		
		int i, j, row_sum, column_sum;	
	
		int[][] SumOfRowCols_arr = {{11, 21, 31}, {41, 51, 61}, {71, 81, 91}};
		
		System.out.println("\nThe Sum of Each Row in a Matrix");	
		for(i = 0; i < SumOfRowCols_arr.length; i++)
		{
			row_sum = 0;
			for(j = 0; j < SumOfRowCols_arr[0].length; j++)
			{
				row_sum = row_sum + SumOfRowCols_arr[i][j];
			}
			System.out.println("The Sum of Matrix Items "
					+ "in Row " + i + " = " + row_sum);
		}
		System.out.println("\nThe Sum of Each Column in a Matrix");
		for(i = 0; i < SumOfRowCols_arr.length; i++)
		{
			column_sum = 0;
			for(j = 0; j < SumOfRowCols_arr[0].length; j++)
			{
				column_sum = column_sum + SumOfRowCols_arr[j][i];
			}
			System.out.println("The Sum of Matrix Items "
					+ "in Column " + i + " = " + column_sum);
		}
	}
}
Program to calculate Sum of Matrix Rows and Column

这个矩阵列和行和代码与上面相同。 但是,这个 代码用于计算矩阵列和行和,它允许用户输入行、列和 矩阵项。

import java.util.Scanner;

public class SumOfMatrixRowsColumns {
	
	private static Scanner sc;
	
	public static void main(String[] args) {
		
		int i, j, rows, columns, row_sum, column_sum;	
		
		sc= new Scanner(System.in);
		
		System.out.println("\n Enter Matrix Rows and Columns :  ");
		rows = sc.nextInt();
		columns = sc.nextInt();
		
		int[][] SumOfRowCols_arr = new int[rows][columns];
		
		System.out.println("\n Please Enter the Matrix Items :  ");
		for(i = 0; i < rows; i++) {
			for(j = 0; j < columns; j++) {
				SumOfRowCols_arr[i][j] = sc.nextInt();
			}		
		}
		
		System.out.println("\nThe Sum of Each Row in a Matrix");	
		for(i = 0; i < SumOfRowCols_arr.length; i++)
		{
			row_sum = 0;
			for(j = 0; j < SumOfRowCols_arr[0].length; j++)
			{
				row_sum = row_sum + SumOfRowCols_arr[i][j];
			}
			System.out.println("The Sum of Matrix Items "
					+ "in Row " + i + " = " + row_sum);
		}
		System.out.println("\nThe Sum of Each Column in a Matrix");
		for(i = 0; i < SumOfRowCols_arr.length; i++)
		{
			column_sum = 0;
			for(j = 0; j < SumOfRowCols_arr[0].length; j++)
			{
				column_sum = column_sum + SumOfRowCols_arr[j][i];
			}
			System.out.println("The Sum of Matrix Items "
					+ "in Column " + i + " = " + column_sum);
		}
	}
}
 Enter Matrix Rows and Columns :  
3 3

 Please Enter the Matrix Items :  
15 25 35
45 55 65
75 85 95

The Sum of Each Row in a Matrix
The Sum of Matrix Items in Row 0 = 75
The Sum of Matrix Items in Row 1 = 165
The Sum of Matrix Items in Row 2 = 255

The Sum of Each Column in a Matrix
The Sum of Matrix Items in Column 0 = 135
The Sum of Matrix Items in Column 1 = 165
The Sum of Matrix Items in Column 2 = 195