每列总和都不在这里工作

时间:2016-11-05 00:01:46

标签: java

package pkg2darray;

    import java.util.*;
    public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) 
    {
        int [][] twodimarr={{1,5,4},{8,7,8},{6,4,3}};
        int lsum=0;
        int rsum=0;
        int ctr1=0;
        int ctr2=2;
        int rowsum=0;
        int columnsum=0;
         int max=0;
        int RowIndex=0;

        for(int i=0;i<twodimarr.length;i++)
        {
            rowsum=0;
            columnsum=0;
            for(int j=0;j<twodimarr.length;j++)
            {
                rowsum+=twodimarr[i][j]; 
                columnsum+=twodimarr[i][j];
                if (i==j)
                {
                    lsum+=twodimarr[i][j];
                }

                if(i==ctr1&&j==ctr2)
                {
                    rsum+=twodimarr[i][j];
                }
            }


            ctr1++;
            ctr2--;
            System.out.println("ROW SUMMATION:"+rowsum); 
               if (rowsum>max)
            {
                max=rowsum;
                RowIndex=i;
            }     
        }
        System.out.println("left diagonal:"+lsum);
        System.out.println("right diagonal:"+rsum);
        System.out.println("column SUMMATION:"+columnsum);
        System.out.println("Max=" + max + " Row with row= "+ RowIndex);
    }
}

2 个答案:

答案 0 :(得分:0)

只需要添加两行。这是相关部分:

for(int j=0;j<twodimarr.length;j++)
{
    rowsum+=twodimarr[i][j];
    columnsum+=twodimarr[j][i];  // for this row(j), add this column(i)
    if (i==j)
    {
        lsum+=twodimarr[i][j];
    }

    if(i==ctr1&&j==ctr2)
    {
        rsum+=twodimarr[i][j];
    }
}

ctr1++;
ctr2--;
System.out.println("ROW SUMMATION:"+rowsum);
System.out.println("column SUMMATION:"+columnsum); // display col sum

这会产生:

ROW SUMMATION:10
column SUMMATION:15
ROW SUMMATION:23
column SUMMATION:16
ROW SUMMATION:13
column SUMMATION:15
left diagonal:11
right diagonal:17
Max=23 Row with row= 1

答案 1 :(得分:0)

这会添加两个int数组。一个用于保存行总和,另一个用于保存列总和。

public static void main(String[] args)
{
  int [][] twodimarr={{1,5,4},{8,7,8},{6,4,3}};
  int lsum=0;
  int rsum=0;
  int ctr1=0;
  int ctr2=2;
  //int rowsum=0;
  //int columnsum=0;
  int max=0;
  int RowIndex=0;
  int [] rowSums = {0,0,0};
  int [] columnSums = {0,0,0};

  for(int row=0; row<twodimarr.length; row++)
  {
    //rowsum=0;
    for(int col=0; col<twodimarr.length; col++)
    {
      rowSums[row] += twodimarr[row][col];
      columnSums[row] += twodimarr[col][row];
      if (row == col)
      {
        lsum += twodimarr[row][col];
      }

      if(row == ctr1 && col == ctr2)
      {
         rsum += twodimarr[row][col];
      }
    }
    ctr1++;
    ctr2--;
    if (rowSums[row] > max)
    {
      max = rowSums[row];
      RowIndex = row;
    }     
  }
  System.out.println("left diagonal: "+lsum);
  System.out.println("right diagonal: "+rsum);
  System.out.println("column SUMMATION: Col: "+ columnSums[0] + " " + columnSums[1] + " " + columnSums[2]);
  System.out.println("row SUMMATION: "+ rowSums[0] + " " + rowSums[1] + " " + rowSums[2]);
  System.out.println("Max=" + max + " Row with row= "+ RowIndex);   
}