我已經鍵入了一個Java程序來計算學生的成績排列。 問題是隻有一個數據集被打印到屏幕上,即學生10的成績。 我正在尋找打印所有學生的所有成績。Java數組顯示被縮短了
有什麼想法可以彌補這一點?
下面的代碼:
public class SummariseGrades
{
public static void main (String[]args)
{
//2d array of student grades
int [][] gradesArray =
{ { 87,96,70 },
{ 68,87,90 },
{ 94,100,90 },
{ 100,81,82},
{ 83,65,85},
{ 78,87,85},
{ 85,75,83},
{ 91,94,100},
{ 76,72,84},
{ 87,93,73} };
//output grades array
outputGrades (gradesArray);
//call methods getMinimum and getMaximum
System.out.printf ("\n %s %d \n %s %d \n \n",
"Lowest grade is", getMinimum (gradesArray),
"Highest grade is", getMaximum (gradesArray)) ;
//output grade distribution chart of all grades on all tests
outputBarChart (gradesArray);
} //end main
//find minimum grade
public static int getMinimum (int grades [][])
{
//assume first element of grades array is the minumum
int lowGrade = grades [0][0];
//loop through rows of grades array
for (int [] studentGrades : grades)
{
//loop throught the columns of current row
for (int grade : studentGrades)
{
//if grade less than lowGrade, assign it to lowGrade
if (grade < lowGrade)
lowGrade = grade ;
} //end inner
}//end outer
return lowGrade; // returns lowest grade
} //end method getMinimum
//find maximum grade
public static int getMaximum (int grades [][])
{
//assume first element is the largest in array
int highGrade = grades [0][0];
//loop through rows of the grades array
for (int[] studentGrades : grades)
{
//loop through columns of the current row
for (int grade: studentGrades)
{
//if grade greater than highGrade then assign it to highGrade
if (grade > highGrade)
highGrade = grade;
} //end inner
} //end outer
return highGrade; // return highest grade
} //end method getMaximum
//determine average grade for particular set of grades
public static double getAverage (int[] setOfGrades)
{
int total = 0; // initialise total
//sum grades for one student
for (int grade : setOfGrades)
total += grade;
//return average of grades
return (double) total/setOfGrades.length;
} //end method for getAverage
//output bar chart displaying overall grade distribution
public static void outputBarChart (int grades[][])
{
System.out.println ("Overall grade distribution:");
//stores the frequency of grades in each range of 10 grades
int[] frequency = new int [11];
// for each grade in the grade book, increment the appropriate frequency
for (int [] studentGrades : grades)
{
for (int grade : studentGrades)
++frequency [ grade/10 ];
} //end outer
//for each grade freq, print bar in chart
for (int count = 0 ; count < frequency.length ; count++)
{
//output bar label
if (count ==10)
System.out.printf ("%5d: ", 100);
else
System.out.printf ("%02d-%02d: ",
count * 10, count * 10 + 9);
//print bar of asterisks
for (int stars = 0 ; stars < frequency [ count ] ; stars++)
System.out.print ("*");
System.out.println(); //start a new line of output
} //end outer for loop
} //end method for outputBarChart
//output contents of the grades array
public static void outputGrades (int grades [][])
{
System.out.println ("The grades are:\n");
System.out.print (" "); //align column heads
// create a column heading for each of the tests
for (int test = 0 ; test < grades [0].length; test ++)
System.out.printf ("Test %d ", test + 1);
System.out.println ("Average"); //student average column heading
//create rows and columns of text representing array grades
for (int student = 0 ; student < grades.length ; student ++)
{
System.out.printf ("Student %2d", student + 1);
for (int test : grades [ student ]) // output student grades
System.out.printf ("%8d", test);
// call method getAverage to calculate the student's average grade
// pass row of grades as the argument to getAverage
double average = getAverage (grades [ student ]) ;
System.out.printf ("%9.2f \r", average);
} // end outer for
} // end method outputGrades
} // end class Summerise Grades
這個類的'}'在哪裏? – 2014-12-07 11:25:16
@CassyHiggins代碼 – CassyHiggins 2014-12-07 11:26:07
的最底部我試着運行你的'outputGrades'方法,並打印了所有10名學生。 – Eran 2014-12-07 11:27:40