总结了二维数组 [英] Summing Up A 2D Array

查看:120
本文介绍了总结了二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于我目前的计划,我想它来计算每列的总和,每行一旦用户进入了他们的所有值。我现在的code似乎只是加倍阵列的价值。这不是我期待的事情。例如,如果用户输入与如下值1 2 3 2 3 4 3 4 5它看起来像我有它在低于我方案格式化的3×3矩阵。 (见上面评论)

Given my current program, I would like it to calculate the sum of each column and each row once the user has entered all their values. My current code seems to be just doubling the value of the array. This is not what I'm looking to do. For example, if the user enters a 3x3 matrix with the following values 1 2 3 2 3 4 3 4 5 it will look like I have it formatted in my program below. (see comment at top)

然后我也想修改code,因此挑选出对角线,并打印出来,让输出将是:主对角线:{1,3,5}

Then I also want to modify the code so it picks out the diagonal and prints that out so that output would read: Main Diagonal: {1,3,5}

推荐答案

有关主对角线

for(int i=0;i<columns;i++)
    {
    for(int j=0;j<rows;j++)
    {
         if(i==j){
          System.out.println(a[i][j]+ "\n"); 
          }
        }

    }

这篇关于总结了二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆