import java.util.Scanner;
public class Test
{
public static void main(String[] args)
{
System.out.println("Enter a 4-by-4 matrix row by row: ");
Scanner input = new Scanner(System.in);
int[][] number = new int[4][4];
for(int i = 0; i < number.length; i++)
for(int j = 0; j < number[i].length; j++)
number[i][j] = input.nextInt();
System.out.println("Sum of the elements in the major diagonal is " + sumMajorDiagonal(number));
}
public static int sumMajorDiagonal(int[][] m)
{
int total = 0;
for(int i = 0; i < m.length; i++)
total += m[i][i];
return total;
}
}
7-2 编程练习题答案
最新推荐文章于 2024-07-07 10:45:00 发布