-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsumMajorDiagonal.java
More file actions
25 lines (24 loc) · 900 Bytes
/
sumMajorDiagonal.java
File metadata and controls
25 lines (24 loc) · 900 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import java.util.Scanner;
public class SumMajorDiagonalMatrix {
public static double sumMajorDiagonal (double [][] m){
double sum = 0;
for (int i =0;i < m.length;i++){
sum = sum + m[i][i];
}
return sum;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入矩阵的行和列: ");
int row = input.nextInt();
int column = input.nextInt();
double[][] matrix = new double[row][column];
System.out.println("请输入矩阵: ");
for (int i = 0;i < matrix.length;i++){
for (int j = 0;j < matrix[i].length;j++){
matrix[i][j] = input.nextDouble();
}
}
System.out.println("Sum of elements in the major diagonal is " + sumMajorDiagonal(matrix));
}
}