我的代码遇到了一些麻烦 - 它不断重写avgs

时间:2014-02-10 19:14:30

标签: java for-loop

我正在编写一个代码,用于输出矩阵的行大小和列大小。 ñ 然后输出行平均值,最后输出列平均值。但是,avg列只会继续打印出来。

/*
 * Programmer: Olawale Onafowokan
 * Date: February 6, 2014
 * Purpose: Prints the row and column averages
 */
class dfsahfa
{
  public static void main(String[] args)
  {

    int [][] scores = {{ 20, 18, 23, 20, 16 },
      { 30, 20, 18, 21, 20 },
      { 16, 19, 16, 53, 24 },
      { 25, 24, 22, 24, 25 }};
    outputArray(scores);
  }

  public static void outputArray(int[][] array)
  { int sum= 0;
    double average=0.00;

    int rowSize = array.length;
    int columnSize = array[0].length;
    int[] colSum =new int[array[0].length];
    int[] Colavg=new int[array[0].length]; ///
    System.out.println("rows="+rowSize+ "cols=" +columnSize); 

    for (int i = 0; i < array.length; i++){   
    for (int j = 0; j < array[i].length; j++){                
        sum = sum + array[i][j];
        colSum[j]+= array[i][j];
        Colavg[j]=colSum[j]/4;
         average=sum/(double)array.length; 

    }

    System.out.println("Print the  averages of the row =" +  average);
}  
for(int k=0;k<colSum.length;k++)
  for(int h=0;h<Colavg.length;h++){


    System.out.println("Print the sum of columns of avg =" + Colavg[h]); }


} 


     }

输出:

运行dfsahfa 行= 4cols = 5

Print the  averages of the row =24.25
Print the  averages of the row =51.5
Print the  averages of the row =83.5
Print the  averages of the row =113.5
Print the sum of columns of avg =22
Print the sum of columns of avg =20
Print the sum of columns of avg =19
Print the sum of columns of avg =29
Print the sum of columns of avg =21
Print the sum of columns of avg =22
Print the sum of columns of avg =20
Print the sum of columns of avg =19
Print the sum of columns of avg =29
Print the sum of columns of avg =21
Print the sum of columns of avg =22
Print the sum of columns of avg =20
Print the sum of columns of avg =19
Print the sum of columns of avg =29
Print the sum of columns of avg =21
Print the sum of columns of avg =22
Print the sum of columns of avg =20
Print the sum of columns of avg =19
Print the sum of columns of avg =29

有一个简单的解决方法吗?为什么要这样做?

2 个答案:

答案 0 :(得分:1)

变化

    for (int i = 0; i < array.length; i++){   
    for (int j = 0; j < array[i].length; j++){                
        sum = sum + array[i][j];
        colSum[j]+= array[i][j];
        Colavg[j]=colSum[j]/4;
        average=sum/(double)array.length; 
    }

    System.out.println("Print the  averages of the row =" +  average);
} 

for (int i = 0; i < array.length; i++){   
    for (int j = 0; j < array[i].length; j++){                
        sum = sum + array[i][j];
        colSum[j]+= array[i][j];
        Colavg[j]=colSum[j]/4;
         average=sum/(double)array.length; 

    }
}
System.out.println("Print the  averages of the row =" +  average);

答案 1 :(得分:0)

在第一个双循环for循环中,您打印出行平均值以及计算列平均值并将它们存储在名为Colavg的数组中。在下一个for循环中,您只想打印存储在数组中的列平均值。您只需要一个for循环来打印这些值。所以消除读取行

for(int k=0;k<colSum.length;k++)

只需循环通过Colavg打印一次这些值。