使用compareTo方法与数组学生的名字和考试成绩排序

问题描述:

我需要使用可比interfact和compareTo方法通过测试成绩按字母顺序排序,然后学生的名单。我挣扎在得到这个在我的应用工作。

I need to use the comparable interfact and a compareTo method to sort a list of students alphabetically and then by test score. I'm struggling at getting this to work in my application.

需要从文本文件中读取的名称的列表。我不知道有多少的名字将是在我的教授除了使用文本文件,这将是小于100,我也应该显示在底部的评分的平均值,以及接下来的消息写入任何学生是低于平均水平15点。我还没有真正得到的消息写一部分,我目前停留在获取名字和分数打印和排序。

The list of names needs to be read from a text file. I don't know how many names will be in the text file that my professor uses except that it will be less than 100. I am also supposed to display the average of the grades at the bottom as well as write a message next to any student that is 15 points below average. I have not really gotten to the writing message part as I am currently stuck on getting the names and scores to print and sort.

该文本文件应该是这个样子:

The text file should look something like this:

斯蒂尔曼安德烈95

Murach乔尔·98

Murach Joel 98

洛道格82

Murach迈克·93

Murach Mike 93

这是我迄今为止......如果有人可以给我,我会把AP preciate了一点方向。谢谢。

This is what I have so far... if someone could give me a little direction I'd appreciate it. Thank you.

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;

    }

}

}

首先,完全删除你的可比接口。使用 可比从JDK

Firstly, delete entirely your Comparable interface. Use Comparable from the JDK.

更改code将该

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

还请注意,没有其他条件收益之后,让我省略了code的冗余部分

Note also that there is no "else" after a conditional return, so I omitted that redundant part of the code