我正在做一个练习来创建一个二维 java 数组,它将每个数字显示为 table 并且每个 row/column 的平均值

I am working on an exercise to create an 2D java array that displays every number as a table and has the averages for each row/column

所以我必须制作这个程序,我能够让程序显示带有行平均值的数组,但我发现很难同时显示列平均值。我的问题是: 1. 我不知道如何添加列值,就像我对行值所做的那样。 2. 我也无法在 table 下方显示平均列值。有人可以帮忙吗?

public class ColRowAvg
{
   public static void main (String args[])
   {
      int avg = 0;
      int[][] arr = {{6,3,4,2}, {3,4,5,7}, {3,6,7,2}, {3, 5, 2, 7}};
      for (int row = 0; row < arr.length; row++)//Cycles through rows
      {
         for (int col = 0; col < arr[row].length; col++)//Cycles through columns
         {
            System.out.printf("%5d", arr[row][col]);
            avg += arr[row][col];
         }
         System.out.printf(" |%2d", avg/arr.length);
         System.out.println(); //Makes a new row
         avg = 0;
      }
      System.out.println();
      for (int col = 0; col < arr.length; col++)//Cycles through rows
      {
         for (int row = 0; row < arr[col].length; row++)//Cycles through columns
         {
            System.out.printf("%5d", arr[row][col]);
            avg += arr[row][col];
         }
         System.out.printf(" |%2d", avg/arr.length);
         System.out.println(); //Makes a new row
         avg = 0;
      }      
   }
}

这就是我现在拥有的。

只需在嵌套的 for 循环中交换行和列