public class T29 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
//定义一个三行三列数组
int [][]arr=new int[3][3];
/*赋值*/
System.out.println("请输入9个整数:");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
arr[i][j]=input.nextInt();
}
}
/*打印矩阵*/
System.out.println("输出的3*3的矩阵是:");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
/*对角线计算*/
int sum1=0;
int sum2=0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
//从左上到右下的对角线计算
if (i==j) {
sum1+=arr[i][j];
}
//从右上到左下的对角线计算
if (i-j==2||j-i==2||i*j==1) {
sum2+=arr[i][j];
}
}
}
System.out.println("矩阵对角线元素之和:"+(sum1+sum2));
}
}数组