首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用带有数组的compareTo方法按名称和测试分数对学生进行排序

使用带有数组的compareTo方法按名称和测试分数对学生进行排序
EN

Stack Overflow用户
提问于 2012-11-10 23:09:40
回答 4查看 24.1K关注 0票数 1

我需要使用类似的插页和compareTo方法来按字母顺序对学生列表进行排序,然后按考试成绩进行排序。我在努力让这个在我的申请中发挥作用。

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

文本文件应该如下所示:

Steelman Andrea 95 穆拉赫·乔尔98 洛道格82 Murach Mike 93

这就是我目前所拥有的..。如果有人能给我一点指导,我会很感激的。谢谢。

代码语言:javascript
复制
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;
    
    }

}

}

EN

回答 4

Stack Overflow用户

发布于 2012-11-10 23:55:23

首先,完全删除您的Comparable接口。使用JDK中的Comparable

将您的代码更改为

代码语言:javascript
复制
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”,所以我省略了代码中的冗余部分。

票数 2
EN

Stack Overflow用户

发布于 2012-11-10 23:34:36

  1. 您不能自己声明可比较的界面。就用它吧。
  2. 您必须在循环中对学生的考试成绩进行汇总,但要在循环外计算平均值和排序数组。
票数 0
EN

Stack Overflow用户

发布于 2012-11-10 23:44:25

在您的代码中,很少有东西需要纠正:

  1. 您的Student类是内部类,因此要创建该类的对象,需要外部类的第一个对象。您可能希望可以在没有外部对象的情况下创建对象的嵌套类(只需将static修饰符添加到Student类)。
  2. 若要使用Arrays.sort()对象对数组进行排序,必须实现java.lang.Comparable接口,而不是由您创建的接口。
  3. 您可以在Comparable<T>中使用泛型,所以尝试用implements Comparable<Student>{来实现您的Student类,就像您的compareTo方法会喜欢的那样 公共int compareTo(学生){//体 而不是: 公共int compareTo(对象o){学生s=(学生) o;
  4. 由于您的数组将包含空值(对于未填充位置的默认值),因此需要防止排序比较器调用空元素上的compareTo。为此,使用Arrays.sort(Object[] a, int fromIndex, int toIndex)版本的排序算法。
  5. 在从文件中收集信息时,再次检查您的逻辑。为了使事情更容易,不要一次做几件事。首先收集所有数据,然后进行统计。
  6. 不要每次向数组添加新数据时对数组进行排序,而是在添加所有数据之后进行排序。
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13326972

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档