gpt4 book ai didi

java - 从 .txt 调用列表作为数组并需要显示平均值

转载 作者:行者123 更新时间:2023-12-01 14:48:06 25 4
gpt4 key购买 nike

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

最佳答案

我发现您的代码存在一些问题。

首先,不需要调用Arrays.sort每次添加 StudentstudentArray 。将此方法调用放置在 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) 时才会打印此行评估结果为真:

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

我相信您现在已经看到问题所在了。

关于java - 从 .txt 调用列表作为数组并需要显示平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15192007/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com