现在行的求和弄好了。想弄列的求和不会。
import java.awt.*;
import javax.swing.*;public class DoubleArray extends JApplet {
   int grades[][] = { { 77, 68, 86, 73, 50 }, 
                      { 96, 87, 89, 81, 66 },
                      { 70, 90, 86, 81, 88 } };   int students, exams;
   String output;
   JTextArea outputArea;   public void init()
   {
      students = grades.length;     // number of students
      exams = grades[ 0 ].length;   // number of exams      // create JTextArea and attach to applet
      outputArea = new JTextArea();
      Container container = getContentPane();
      container.add( outputArea );      // build output string
      output = "The array is:\n";
      buildString();      for ( int counter = 0; counter < students; counter++ ) 
         output += "\nTotal for student " + counter + " is " +
            average( grades[ counter ] );  // pass one row of array grades      // change outputArea's display font
      outputArea.setFont( new Font( "Monospaced", Font.PLAIN, 12 ) );      // place output string in outputArea
      outputArea.setText( output );   } // end method init
  
   public double average( int setOfGrades[] )
   {
      int total = 0;  // initialize total
 
      // sum grades for one student
      for ( int count = 0; count < setOfGrades.length; count++ )
         total += setOfGrades[ count ];      // return average of grades
      return ( double ) total ;
  // setOfGrades.length;   } // end method average   // build output string
   public void buildString()
   {
      output += "           ";  // used to align column heads      // create column heads
      for ( int counter = 0; counter < exams; counter++ ) 
         output += "[" + counter + "]  ";      // create rows/columns of text representing array grades
      for ( int row = 0; row < students; row++ ) {
         output += "\ngrades[" + row + "]   ";         for ( int column = 0; column < exams; column++ ) 
            output += grades[ row ][ column ] + "   ";
      }   } // end method buildString} // end class DoubleArray

解决方案 »

  1.   

    一定要用 public double average( int setOfGrades[] ) 来做列的和吗?如果是这样,就必须做行列转换不是的话,你手写应该应该很容易啊你到底是哪里不懂
      

  2.   

    一定要用 public double average( int setOfGrades[] ) 来做列的和吗?如果是这样,就必须做行列转换不是的话,你手写应该应该很容易啊你到底是哪里不懂
      

  3.   

    columnSum(int grade[][],int column){     //返回二维数组第column列的和
       int sum=0;
       for(int i=0;i<grade.length;i++){
           sum+=grade[i][column];
       }
       return sum;
    }
      

  4.   

    int columnSum(int grade[][],int column){
      

  5.   

    public double average( int setOfGrades[] ) 我是想用这个来求列的和。不懂的如何转换
      

  6.   

    直接对二维数组的列求和就行,不用再转为一维数组。columnSum(int grade[][],int column){     //返回二维数组第column列的和
       int sum=0;
       for(int i=0;i<grade.length;i++){
           sum+=grade[i][column];
       }
       return sum;
    }
      

  7.   

    为求个和还要转置一下,太累了,不转置,你用public double average( int setOfGrades[] ) 不能实现.
    除非你从grade[][]中把某列的值用一个个拣出来,放到一个一维数组中,再传给average方法,也累。多犯手续。
      

  8.   

    我在上面添加了  for ( int hang = 0; hang < students; hang++ ) 
             output += "\nTotal for hang " + hang + " is " +
                columnSum( grades[] ); 
    和goodmrning 给的代码后编译显示:
    类型 DoubleArray 中的方法 columnSum(int[][], int)对于参数(int[])不适用 现在要怎么修改?
      

  9.   

    for ( int hang = 0; hang < exams; hang++ )   //不是students
            output += "\nTotal for hang " + hang + " is " + 
                columnSum( grades);               //不是grades[]