根据控制台文本内容的提示输入9个数字,这九个数字将会组成一个3*3的矩阵,程序会计算出你输入的矩阵的对角线之和。
结果演示
代码演示
package com.five;import java.util.Scanner;public class Juzhen {public static Scanner input = new Scanner(System.in);public static void main(String[] args) {int sum = 0;System.out.println("请输入九个整数以求对角线之和");int [][] arrInt = new int[3][3];for(int i=0;i<arrInt.length;i++){for(int j=0;j<arrInt.length;j++){arrInt[i][j] = input.nextInt();}}System.out.println("你输入的九位数矩阵为:");for(int i=0;i<arrInt.length;i++){for(int j=0;j<arrInt.length;j++){System.out.println(arrInt[i][j]+" ");}System.out.println();}for(int i=0;i<arrInt.length;i++){for(int j=0;j<arrInt.length;j++){if(i == j || i == arrInt.length - 1 - j){sum = sum + arrInt[i][j];}if(i == 1 && j == 1){sum = sum + arrInt[i][j];}}}System.out.println("对角线之和为:"+sum);}}
了解更多关注我哟!!!