现在行的求和弄好了。想弄列的求和不会。
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
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
int sum=0;
for(int i=0;i<grade.length;i++){
sum+=grade[i][column];
}
return sum;
}
int sum=0;
for(int i=0;i<grade.length;i++){
sum+=grade[i][column];
}
return sum;
}
除非你从grade[][]中把某列的值用一个个拣出来,放到一个一维数组中,再传给average方法,也累。多犯手续。
output += "\nTotal for hang " + hang + " is " +
columnSum( grades[] );
和goodmrning 给的代码后编译显示:
类型 DoubleArray 中的方法 columnSum(int[][], int)对于参数(int[])不适用 现在要怎么修改?
output += "\nTotal for hang " + hang + " is " +
columnSum( grades); //不是grades[]