1

我需要使用可比较的 interfact 和 compareTo 方法按字母顺序对学生列表进行排序,然后按考试成绩排序。我正在努力让它在我的应用程序中工作。

需要从文本文件中读取名称列表。我不知道我的教授使用的文本文件中有多少个名字,除了它会少于 100 个。我还应该在底部显示平均成绩,并在任何旁边写一条消息低于平均水平 15 分的学生。我还没有真正进入写作信息部分,因为我目前正忙于获取姓名和分数以进行打印和排序。

文本文件应如下所示:

钢铁侠安德里亚 95

穆拉赫乔尔 98

洛道格 82

穆拉赫迈克 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;
    
    }

}

}

4

4 回答 4

2

首先,完全删除您的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” return,因此我省略了代码的冗余部分

于 2012-11-10T23:55:23.250 回答
0
  1. 你不能自己声明 Comparable 接口。只需使用它。
  2. 您必须在循环内对学生的考试成绩求和,但在循环外计算平均值,对数组进行排序。
于 2012-11-10T23:34:36.070 回答
0

您的代码中有几处需要更正:

  1. 您的Student类是内部类,因此要创建该类的对象,您需要外部类的第一个对象。您可能想要可以在没有外部对象的情况下创建对象的嵌套类(只需static向类添加修饰符Student
  2. 使用对象对数组进行排序Arrays.sort()必须实现java.lang.Comparable接口,而不是您创建的接口。
  3. 你可以使用泛型,Comparable<T>所以尝试用这种方法实现你的Student类,你的方法会喜欢implements Comparable<Student>{compareTo

    public int compareTo(Student s){//body 
    

    代替:

    public int compareTo(Object o){ 
        Student s = (Student) o; 
    
  4. 由于您的数组将包含空值(未填充位置的默认值),您需要防止排序比较器compareTo在空元素上调用。为此,请使用Arrays.sort(Object[] a, int fromIndex, int toIndex)排序算法的版本。

  5. 从文件中收集信息时再次检查您的逻辑。为了让事情变得更容易,不要一次做几件事。首先收集所有数据,然后进行统计。

  6. 不要在每次添加新数据时对数组进行排序,在添加所有数据后进行。
于 2012-11-10T23:44:25.693 回答
0
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class CH11AS8App {

    public static void main(String[] args) throws Exception {

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

        List<Student> studentArray = new ArrayList<Student>();

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

        String line;

        FileReader reader = new FileReader("src//chapt11//ch11AS8data.txt");
        BufferedReader bufferedReader = new BufferedReader(reader);
        String[] split;

        while ((line = bufferedReader.readLine()) != null) {
            split = line.split("\\s+");
            lastName = split[0];
            firstName = split[1];
            examScore = Integer.valueOf(split[2]);

            studentArray.add(new Student(firstName, lastName, examScore));
            double sum = 0.0;
        }
        Collections.sort(studentArray);

        System.out.println();

        System.out.println("sorted:" + studentArray);

        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);

        }
    }

    static class Student implements Comparable<Student> {

        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(Student s) {

            if (this.firstName.equalsIgnoreCase(s.firstName)) {
                if (this.lastName.equalsIgnoreCase(s.lastName)) {
                    return this.examScore - s.examScore;
                } else {
                    return this.lastName.compareTo(s.lastName);
                }
            } else {
                return this.firstName.compareTo(s.firstName);
            }


        }

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

        }

    }
}
于 2016-10-23T11:05:54.713 回答