定义一个Student数组,其中保存学生的基本信息,包括姓名,学号,性别,还分别保存三门课程的成绩及三门课程对应的学分,试编程计算这三门课程的学分积,并按学分积的降幕进行排序,输出排序后的结果。

解决方案 »

  1.   

    定义一个Student类  然后有姓名,性别和学号这三个属性  并且用一个HashMap来保存各门课程及成绩  然后就是排序输出
      

  2.   

    首先写一个Student类,包括姓名,性别,学号,然后用HashMap类保存各门成绩,学分。
      

  3.   

    课程也可以抽象为一个course 的class
    class course {
      String name;
      float score;
      float credit;
    }
      

  4.   

    好像是练习题啊,看看代码吧。关键是排序,我使用的TreeSet的比较器Comparator接口来实现的/**
     * 课程
     * @author kreadk
     *
     */
    public class Course {
    private String courseName;
    //学分
    private int credit;

    public Course(String courseName, int credit) {
    this.courseName = courseName;
    this.credit = credit;
    }
    public String getCourseName() {
    return courseName;
    }
    public void setCourseName(String courseName) {
    this.courseName = courseName;
    }
    public int getCredit() {
    return credit;
    }
    public void setCredit(int credit) {
    this.credit = credit;
    }
    }
      

  5.   

    /**
     * 实现比较器接口
     * 
     */
    public class Student implements Comparator {
    private String name;
    private int stuNo;
    private String gender;
    // 课程对象 <--> 成绩
    private HashMap<Course, Float> courses;
    // 总学分
    private int totalCredit; public int getTotalCredit() {
    totalCredit = 0;
    for (Map.Entry<Course, Float> entry : this.getCourses().entrySet()) {
    // 及格了?
    if (entry.getValue() >= 60) {
    totalCredit += entry.getKey().getCredit();
    }
    }
    return totalCredit;
    } public void setTotalCredit(int totalCredit) {
    this.totalCredit = totalCredit;
    } public String getName() {
    return name;
    } public void setName(String name) {
    this.name = name;
    } public int getStuNo() {
    return stuNo;
    } public void setStuNo(int stuNo) {
    this.stuNo = stuNo;
    } public String getGender() {
    return gender;
    } public void setGender(String gender) {
    this.gender = gender;
    } public HashMap<Course, Float> getCourses() {
    return courses;
    } public void setCourses(HashMap<Course, Float> courses) {
    this.courses = courses;
    } /**
     * 按照总学分降序排序
     */
    public int compare(Object o1, Object o2) {
    Student stu1 = (Student) o1;
    Student stu2 = (Student) o2;
    int res = stu2.getTotalCredit() - stu1.getTotalCredit();
    return res;
    }}
      

  6.   

    //测试类
    public class Test {
    public static void main(String[] args) {
    Student[] stus = new Student[3];
    Course c1 = new Course("Java", 5);
    Course c2 = new Course("Struts", 4);
    Course c3 = new Course("EJB", 3); Random random = new Random();
    for (int i = 0; i < 3; i++) {
    Student stu = new Student();
    stu.setName("stu" + i);
    stu.setGender("male");
    stu.setStuNo(i);
    HashMap<Course, Float> courses = new HashMap<Course, Float>(0);
    //随机生成课程的成绩
    courses.put(c1, random.nextInt(150) + 0f);
    courses.put(c2, random.nextInt(150) + 0f);
    courses.put(c3, random.nextInt(150) + 0f);
    stu.setCourses(courses);
    stus[i] = stu;
    } // TreeSet会自动按照比较器排序
    TreeSet<Student> students = new TreeSet<Student>(new Student());
    students.addAll(Arrays.asList(stus));
    for (Student student : students) {
    // 输出
    System.out.println(student.getTotalCredit());
    }
    }
    }
      

  7.   

    呵呵,顶下,这个问题不难,do it yourself~~