文件名称:求主对角线之和-devexpress中gridcontrol使用技巧总结-详解(图文并茂)
文件大小:4.48MB
文件格式:PDF
更新时间:2024-06-28 10:37:24
java 入门资料 java入门资料
2.10 求主对角线之和 2.10.1 题目:求一个 3*3 矩阵对角线元素之和。 2.10.2 源程序 import java.util.Scanner; public class DuiJiaoXian { public static void main(String[] args) { Scanner s = new Scanner(System.in); int[][] a = new int[3][3]; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { a[i][j] = s.nextInt(); } } System.out.println("输入的3 * 3 矩阵是:"); for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { System.out.print(a[i][j] + " "); } System.out.println(); } int sum = 0; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (i == j) { sum += a[i][j]; } }