我需要使用类似的插页和compareTo方法来按字母顺序对学生列表进行排序,然后按考试成绩进行排序。我在努力让这个在我的申请中发挥作用。
名称列表需要从文本文件中读取。我不知道在我的教授使用的文本文件中会有多少个名字,除了它会少于100个。我还应该显示最低分的平均成绩,并在低于平均分15分的学生旁边写一条信息。我还没有真正到写作信息部分,因为我目前困在获得姓名和分数,以打印和排序。
文本文件应该如下所示:
Steelman Andrea 95 穆拉赫·乔尔98 洛道格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;
}
}}
发布于 2012-11-10 23:55:23
首先,完全删除您的Comparable接口。使用JDK中的Comparable。
将您的代码更改为
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
}
}还请注意,在条件return之后不存在“there”,所以我省略了代码中的冗余部分。
发布于 2012-11-10 23:34:36
发布于 2012-11-10 23:44:25
在您的代码中,很少有东西需要纠正:
Student类是内部类,因此要创建该类的对象,需要外部类的第一个对象。您可能希望可以在没有外部对象的情况下创建对象的嵌套类(只需将static修饰符添加到Student类)。Arrays.sort()对象对数组进行排序,必须实现java.lang.Comparable接口,而不是由您创建的接口。Comparable<T>中使用泛型,所以尝试用implements Comparable<Student>{来实现您的Student类,就像您的compareTo方法会喜欢的那样
公共int compareTo(学生){//体
而不是:
公共int compareTo(对象o){学生s=(学生) o;compareTo。为此,使用Arrays.sort(Object[] a, int fromIndex, int toIndex)版本的排序算法。https://stackoverflow.com/questions/13326972
复制相似问题