.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;
}
}