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每次添加 aStudentstudentArray. 将此方法调用放在while排序完整数组的末尾之外,只需一次调用该方法。

其次,更重要的是,您希望如何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]);
}

并且仅score <= (average - 10)评估为 true时才会打印此行:

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

我相信你现在看到了问题。

于 2013-03-03T23:20:50.010 回答