2014-12-07 66 views
-1

我已經鍵入了一個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 
+0

這個類的'}'在哪裏? – 2014-12-07 11:25:16

+2

@CassyHiggins代碼 – CassyHiggins 2014-12-07 11:26:07

+0

的最底部我試着運行你的'outputGrades'方法,並打印了所有10名學生。 – Eran 2014-12-07 11:27:40

回答

1

即使我可以重現此錯誤。問題出在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 

OR

湯姆建議

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 

,謹供參考

前:

[email protected]:~$ javac SummariseGrades.java 
[email protected]:~$ 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: *** 

後:

[email protected]:~$ javac SummariseGrades.java 
[email protected]:~$ 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: *** 
+0

他不需要println調用。他只需要用'\ n'替換'\ r'。 – Tom 2014-12-07 13:11:34

+0

@Tom是的...我沒有認識到這一點。 – 2014-12-07 13:16:46

+1

無關緊要。你已經找到了問題並提供瞭解決方案。這就是重要的:)。 – Tom 2014-12-07 13:20:11

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: *** 

是這樣的結果,你想要什麼?

+0

是的,這正是我在找什麼由於某種原因,我沒有得到任何輸出爲學生1至9.剛剛得到輸出爲學生10! – CassyHiggins 2014-12-07 11:39:10

+0

我在TextPad中試過它,它不能正常工作......但它似乎在Eclipse中工作!我很困惑! – CassyHiggins 2014-12-07 12:03:13

+0

我在eclipse中運行它,它工作。 – hossein 2014-12-08 09:57:35