gpt4 book ai didi

java - 使用带有数组的compareTo方法按姓名和考试成绩对学生进行排序

转载 作者:行者123 更新时间:2023-11-30 04:36:41 25 4
gpt4 key购买 nike

我需要使用比较接口(interface)和compareTo 方法按字母顺序对学生列表进行排序,然后按测试分数进行排序。我正在努力让它在我的应用程序中发挥作用。

需要从文本文件中读取名称列表。我不知道我的教授使用的文本文件中会有多少个名字,只是它会少于 100 个。我还应该在底部显示成绩的平均值,并在任何名字旁边写一条消息比平均分低 15 分的学生。我还没有真正开始编写消息部分,因为我目前一直在打印和排序姓名和分数。

文本文件应如下所示:

Steelman Andrea 95

Murach Joel 98

Lowe Doug 82

Murach Mike 93

这就是我到目前为止所拥有的......如果有人能给我一点指导,我将不胜感激。谢谢。

package chapt11;
import java.io.FileReader;
import java.util.Arrays;
importjava.util.Scanner;
public class CH11AS8App {

/**
* @param args
*/
public static void main(String[] args) throws Exception {

System.out.println("Welcome to the Student Scores Application.");
System.out.println();

Scanner aScanner = new Scanner(new FileReader(
"src//chapt11//ch11AS8data.txt"));

Student [] studentArray;

String lastName;
String firstName;
int examScore = 0;
double average = 0;

int nStudent = 100; //array size not set unit run time
studentArray = new Student[nStudent];

for (int i=0; i<nStudent; i++)
{
System.out.println();

while (aScanner.hasNext()) {
lastName = aScanner.next();
firstName = aScanner.next();
examScore = aScanner.nextInt();

System.out.println("Student " + nStudent++ + " " + firstName
+ " " + lastName + " " + +examScore);

studentArray[i] = new Student(lastName, firstName, examScore);


}
double sum = 0.0;
sum += examScore;
average = sum/nStudent;

Arrays.sort(studentArray);

System.out.println();

for (Student aStudent: studentArray)
{
System.out.println(aStudent);

if (examScore<= (average-10))
{
System.out.println ("Score 10 points under average");
}
System.out.println("Student Average:" +average);

}
}
}
public interface Comparable {
int compareTo(Object o);
}

class Student implements Comparable {

private String firstName;
private String lastName;
private int examScore;

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

// Get & Set Methods
public int getExamScore() {
return examScore;
}

public String getFirstName() {
return firstName;
}

public String getLastName() {
return lastName;
}

@Override
public int compareTo(Object o) {
Student s = (Student) o;

if (s.lastName.equals(lastName)) {
return firstName.compareToIgnoreCase(s.firstName);
} else {
return lastName.compareToIgnoreCase(s.lastName);
}
}

public String toString() {
return lastName + ", " + firstName + ": " + examScore;

}

}

}

最佳答案

首先,完全删除您的Comparable 界面。使用Comparable来自 JDK。

将您的代码更改为此

public static class Student implements Comparable<Student> {

// rest of class omitted

@Override
public int compareTo(Student s) {
if (s.lastName.equals(lastName)) {
return firstName.compareToIgnoreCase(s.firstName);
}
return lastName.compareToIgnoreCase(s.lastName);return
}
}

另请注意,条件返回后没有“else”,因此我省略了代码的冗余部分

关于java - 使用带有数组的compareTo方法按姓名和考试成绩对学生进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13326972/

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