求一个3*3矩阵对角线元素之和。

public class Example29 {
    public static void main(String[] args) {
        int[][] a = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
        sum(a);
    }

public static void sum(int[][] a) {
        System.out.println("您输入的3*3矩阵是:");
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < a[i].length; j++) {
                System.out.print(a[i][j] + "\t");
            }
            System.out.println();
        }
        int sum = 0;
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < a[i].length; j++) {
                if (i == j) {
                    sum += a[i][j];
                }
            }
        }
        System.out.println("对角线之和是:" + sum);
    }
}

Java经典编程题50道之二十九的更多相关文章

  1. Java经典编程题50道之二十五

    一个5位数,判断它是不是回文数.即12321是回文数,个位与万位相同,十位与千位相同. public class Example25 {    public static void main(Stri ...

  2. Java经典编程题50道之二十

    有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前20项之和. public class Example20 {    public static void ma ...

  3. Java经典编程题50道之二十六

    请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继续判断第二个字母. public class Example26 {    public static void main(Stri ...

  4. Java经典编程题50道之二十八

    对10个数进行排序. public class Example28 {    public static void main(String[] args) {        int[] s = { 5 ...

  5. Java经典编程题50道之二十四

    有5个人坐在一起,问第5个人多少岁,他说比第4个人大2岁.问第4个人岁数,他说比第3个人大2岁. 问第三个人,他说比第2人大两岁.问第2个人, 说比第一个人大两岁.最后问第一个人,他说是10岁. 请问 ...

  6. Java经典编程题50道之二十二

    利用递归方法求5!. public class Example22 {    public static void main(String[] args) {        int n = 5;   ...

  7. Java经典编程题50道之二十七

    求100之内的素数. public class Example27 {    public static void main(String[] args) {        prime();    } ...

  8. Java经典编程题50道之二十三

    给一个不多于5位的正整数,要求:①求它是几位数:②逆序打印出各位数字. public class Example23 {    public static void main(String[] arg ...

  9. Java经典编程题50道之二十一

    求1+2!+3!+...+20!的和. public class Example21 {    public static void main(String[] args) {        sum( ...

随机推荐

  1. CCF系列之最大的矩形(201312-3)

    试题名称: 最大的矩形 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 在横轴上放了n个相邻的矩形,每个矩形的宽度是1,而第i(1 ≤ i ≤ n)个矩形的高度是hi.这n个矩 ...

  2. main函数的两个参数

    #include "stdio.h" int main(int argc ,char **argv) { printf("argc = %d \n",argc) ...

  3. 一个使用物理引擎的WebGL3D场景

    这是一个类似第三人称射击游戏(TPS)的3D场景,可以通过https://ljzc002.github.io/FPS2/index.html访问.场景运行效果如下图: 场景环境由一个天空盒和一个地面网 ...

  4. 防盗链[referer]

    原文出处:http://www.cnblogs.com/devilfree/archive/2012/09/11/2680914.html 总结一下今天学习防盗链Filter的一些知识点: 防盗链要实 ...

  5. 新awk整理

    总感觉上一篇awk的总结几乎是照着man翻译过来的,惨不忍睹 无意间在互联网上有找到了宝贵的资料 感觉整理的很好,想着照着这个来重新写下,对照新的man更新下吧,只是总是在改变的 一.awk简介二.a ...

  6. linux安全篇

    笔者Q:972581034 交流群:605799367.有任何疑问可与笔者或加群交流 1.限制用户su 限制能su到root的用户. 操作步骤 使用命令 vi /etc/pam.d/su修改配置文件, ...

  7. 【转】shell学习笔记(四)——条件测试

    1 test 条件检测 当我要检测系统上面某些文件或者是相关的属性时,利用 test 这个命令来工作真是好用得不得了, 举例来说,我要检查 /home/oracle/zy是否存在时,使用: test ...

  8. awk的sub函数和gsub函数的用法

    1. sub函数 [root@nhserver1 10]# echo "a b c 2011-11-22 a:d" | awk 'sub(/-/,"",$4)' ...

  9. 【转】5 Best Place to Learn Linux – Linux Tutorial Sites

    Linux have amazed every tech guy and make them curious to hands on Linux. Many of us not feel Linux ...

  10. Git分支高级管理[四]

    标签(linux): git 笔者Q:972581034 交流群:605799367.有任何疑问可与笔者或加群交流 切换分支 git checkout 撤销对文件的修改 git checkout -- ...