我输入了一个Java程序,用于计算学生的成绩数组。 问题是只有一个数据集被打印到屏幕上,即学生10的成绩。 我打算为所有学生打印所有成绩。
有什么想法可以解决这个问题吗?
以下是代码:
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
答案 0 :(得分:2)
即使我可以重现此错误。问题出在printf
上。像这样编辑代码,它甚至可以在textpad中工作
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 );
/*Add this line */
System.out.println ("");
} // end outer for
或强>
汤姆建议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 ] ) ;
/* change this line */
System.out.printf ("%9.2f \n", average );
} // end outer for
供您参考
在:
bhargav@bhargav:~$ javac SummariseGrades.java
bhargav@bhargav:~$ java SummariseGrades
The grades are:
Test 1 Test 2 Test 3 Average
Student 10 87 93 73 84.33
Lowest grade is 65
Highest grade is 100
Overall grade distribution:
00-09:
10-19:
20-29:
30-39:
40-49:
50-59:
60-69: **
70-79: ******
80-89: ************
90-99: *******
100: ***
之后:
bhargav@bhargav:~$ javac SummariseGrades.java
bhargav@bhargav:~$ java SummariseGrades
The grades are:
Test 1 Test 2 Test 3 Average
Student 1 87 96 70 84.33
Student 2 68 87 90 81.67
Student 3 94 100 90 94.67
Student 4 100 81 82 87.67
Student 5 83 65 85 77.67
Student 6 78 87 85 83.33
Student 7 85 75 83 81.00
Student 8 91 94 100 95.00
Student 9 76 72 84 77.33
Student 10 87 93 73 84.33
Lowest grade is 65
Highest grade is 100
Overall grade distribution:
00-09:
10-19:
20-29:
30-39:
40-49:
50-59:
60-69: **
70-79: ******
80-89: ************
90-99: *******
100: ***
答案 1 :(得分:0)
我测试了你的代码并得到了这个输出:
The grades are:
Test 1 Test 2 Test 3 Average
Student 1 87 96 70 84.33
Student 2 68 87 90 81.67
Student 3 94 100 90 94.67
Student 4 100 81 82 87.67
Student 5 83 65 85 77.67
Student 6 78 87 85 83.33
Student 7 85 75 83 81.00
Student 8 91 94 100 95.00
Student 9 76 72 84 77.33
Student 10 87 93 73 84.33
Lowest grade is 65
Highest grade is 100
Overall grade distribution:
00-09:
10-19:
20-29:
30-39:
40-49:
50-59:
60-69: **
70-79: ******
80-89: ************
90-99: *******
100: ***
这个结果是你想要的吗?