2016-12-06 44 views
1

我創建一個數組來打印測試成績和他們的臨界值。然而,每一次我的輸出是這樣的:我如何輸出這個陣列測試成績的?

[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 

我應該怎麼改變,因此輸出的測試成績和截止?

這裏是我的2類:

public class Grade 
{ 
    private String grades; 
    private int cutoff; 
    //----------------------------------------------------------------- 
    // Stores the possible grades and their numeric lowest value. 
    //----------------------------------------------------------------- 
    public Grade (String average, int lowvalue) 
    { 
    grades = average; 
    cutoff = lowvalue; 
    } 
    //----------------------------------------------------------------- 
    // Returns the possible grades and their lowest numeric value. 
    //----------------------------------------------------------------- 
    public String getGrades (String grades, int cutoff) 
    { 
    return grades + "\t" + cutoff; 
    } 
} 

Driver類:

public class GradeRange 
{ 
//----------------------------------------------------------------- 
// Stores the possible grades and their numeric lowest value, 
// then prints them out. 
//----------------------------------------------------------------- 
    public static void main (String[] args) 
    { 
    Grade[] score = new Grade[12]; 
    score[0] = new Grade ("A", 95); 
    score[1] = new Grade ("A-", 90); 
    score[2] = new Grade ("B+", 87); 
    score[3] = new Grade ("B", 83); 
    score[4] = new Grade ("B-", 80); 
    score[5] = new Grade ("C+", 77); 
    score[6] = new Grade ("C", 73); 
    score[7] = new Grade ("C-", 70); 
    score[8] = new Grade ("D+", 67); 
    score[9] = new Grade ("D", 63); 
    score[10] = new Grade ("D-", 60); 
    score[11] = new Grade ("F", 0); 
    for (int index = 0; index < score.length; index++) 
     System.out.println (score[index]); 
    } 
} 
+0

什麼是所期望的輸出 – Nkosi

+1

'得分[指數]'將是一個級對象的一個​​實例。嘗試覆蓋您的成績對象上的toString方法:http://stackoverflow.com/questions/10734106/how-to-override-tostring-properly-in-java –

+0

創建一個toString方法,它返回與getGrades相同的東西,然後調用它數組項 – Nkosi

回答

1

添加toString方法甲級類

public String toString() { 
    return grades + "\t" + cutoff; 
} 

,並用它在for循環

for (int index = 0; index < score.length; index++) 
    System.out.println (score[index].toString()); 
+0

它的工作原理。非常感謝你! –