编程语言
首页 > 编程语言> > java 题目:求一个 3*3 矩阵对角线元素之和

java 题目:求一个 3*3 矩阵对角线元素之和

作者:互联网

import java.util.Scanner;

public class Pro5 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner in = new Scanner(System.in);
		int sum = 0;
		int a[][] = new int [3][3];
		//输入矩阵
		System.out.println("请输入一个3*3的矩阵:");
		for (int i = 0; i < 3; i++) {
			for (int j = 0; j < 3; j++) {
				a[i][j] = in.nextInt();
			}
		}
		//计算对角线元素之和
		for (int i = 0; i < 3; i++) {
			sum += a[i][i];
		}
		System.out.println("该矩阵对角线元素之和为:"+sum);
	}

}

标签:java,Scanner,int,sum,矩阵,System,++,对角线
来源: https://www.cnblogs.com/rainbow7colours/p/16339534.html