0

.txt ファイルは次のようになります。

Gator Ali 85
Vator Ella 75
Beam James 95
Lastin Class 55

他の人がここで同様のコードを取得しようとしているのを見たことがありますが、どれも役に立ちませんでした。
平均を取得できます。ただし、スコアが平均を 10 ポイント下回る場合は、印刷する必要もあります。
これが私の出力です:実行:

学生スコア アプリケーションへようこそ。

Ali Gator 85
Ella Vator 75
James Beam 95
Class Lastin 55
Beam, James: 95
Gator, Ali: 85
Lastin, Class: 55
Vator, Ella: 75

Your score is 10 points or more below the class average

Class Average: 77.5

これが私のコードです:

public static void main(String[] args) throws Exception {

    Scanner aScanner = new Scanner(new FileReader("src//chapt11//Studentdata.txt"));
    System.out.println("Welcome to the Student Scores Application.\n");

    int nStudent = 100;
    Student[] studentArray = new Student[nStudent];
    int counter = 0;
    while (aScanner.hasNext()) {
        String lastName = aScanner.next();
        String firstName = aScanner.next();
        int score = aScanner.nextInt();
        System.out.println(firstName + " " + lastName + " " + score);
        studentArray[counter++] = new Student(lastName, firstName, score);  
        Arrays.sort(studentArray, 0, counter); 
    } 

    System.out.println();

    for(int j = 0; j < counter; j++){  
        System.out.println(studentArray[j]);
    }

    double sum = 0;
    for(int j = 0; j < counter; j++){    
        sum += studentArray[j].getExamScore();
    }
    double average = (sum*1.0)/counter;

    for(int j = 0; j < counter; j++){    
        int score = studentArray[j].getExamScore();
        if (score <= (average - 10)){
             System.out.println("Your score is 10 points or more below the class average");
        } 
    }
    System.out.println();
    System.out.println("Class Average: " + average);

    System.out.println();
    aScanner.close();  
}

class Student implements Comparable<Student> {
private String firstName;
private String lastName;
private int score;

public Student(String firstName, String lastName, int score) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.score = score;
}

public int getExamScore() {
    return score;
}

public String getFirstName() {
    return firstName;
}

public String getLastName() {
    return lastName;
}

@Override
public int compareTo(Student s) {
    if(!firstName.equalsIgnoreCase( s.firstName)) {
        return firstName.compareToIgnoreCase(s.firstName);
    }
    if(!lastName.equalsIgnoreCase(s.lastName)) {
        return lastName.compareToIgnoreCase(s.lastName);
    }
        return score - s.score;
    }
 @Override 
 public String toString(){ 
        return firstName + ", " + lastName + ": "+ score;
    }
}
4

1 に答える 1

1

あなたのコードにはいくつかの問題があります。

まず、 に を追加するArrays.sortたびに呼び出す必要はありません。このメソッド呼び出しは、メソッドへの 1 回の呼び出しだけで完全な配列をソートする末尾の外側に配置します。StudentstudentArraywhile

次に、さらに重要なことですが、Studentオブジェクトをどのように比較しますか?

compareTo内部のメソッドStudentは意味がありません。学生をスコアのみでソートしたい場合は、次のようにする必要があります。

@Override
public int compareTo(Student s) {
    return this.score - s.score;
}

期待される出力に関する限り、これが起こっていることです。

次の行Studentは、配列に追加されるたびにそれぞれを出力します。

System.out.println(firstName + " " + lastName + " " + score);

この部分は、各Studentオブジェクトを再度出力します。

for (int j = 0; j < counter; j++) {
    System.out.println(studentArray[j]);
}

そして、この行はtrue と評価された場合にのみ出力されます:score <= (average - 10)

System.out.println("Your score is 10 points or more below the class average");

私は今、あなたが問題を見ていると信じています。

于 2013-03-03T23:20:50.010 に答える