用集合框架做一个成绩输出表

TaoCesc 2016-10-16 03:12:06
某专业有60名学生分成两个班级,三门必修课C语言、微积分、大学英语。
假设每门课成绩随机产生,要求输出
1.大学英语成绩单
2.C语言排序成绩单
3.随机抽取5个微积分不及格的同学重新考试
public class Score {
private Student student;
private Course course;
private double score;
//省略了getter和setter方法
//省略了hashcode和equals方法


public class Course {
private String name;
//省略了getter和setter方法
//省略了hashcode和equals方法


public class Student {
private String name;
private int classNo;
//省略了getter和setter方法
//省略了hashcode和equals方法

public class GenScore {

public static Map<String, List<Score>> InitScore() {
List<Student> studentList = new ArrayList<Student>();
for (int i = 0; i < 60; i++) {
if (i < 30) {
studentList.add(new Student("student" + i, 1));
} else {
studentList.add(new Student("student" + i, 2));
}
}
Course yuyan = new Course("C语言");
Course shuxue = new Course("微积分");
Course yingyu = new Course("大学英语");

List<Score> yuyanScores = new ArrayList<Score>();
List<Score> shuxueScores = new ArrayList<Score>();
List<Score> yingyuScores = new ArrayList<Score>();
Random rand = new Random();
for (int i = 0; i < 60; i++) {
Student stu = studentList.get(i);
Score yuyanScore = new Score(stu, yuyan, rand.nextInt(100));
Score shuxueScore = new Score(stu, shuxue, rand.nextInt(100));
Score yingyuScore = new Score(stu, yingyu, rand.nextInt(100));
yuyanScores.add(yuyanScore);
shuxueScores.add(shuxueScore);
yingyuScores.add(yingyuScore);
}

Map<String, List<Score>> scores = new HashMap<String, List<Score>>();
scores.put("C语言", yuyanScores);
scores.put("微积分", shuxueScores);
scores.put("大学英语", yingyuScores);
return scores;

}


这是照着书写的,但是不会输出,请问一下该怎么写主函数呢
...全文
334 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
heanyinqin 2016-10-17
  • 打赏
  • 举报
回复
其中Student类 和course类和楼主的一样就是加了getset方法,就不贴了。
heanyinqin 2016-10-17
  • 打赏
  • 举报
回复
public class GenScore  {

    public static Map<String, List<Score>> InitScore() {
        List<Student> studentList = new ArrayList<Student>();
        for (int i = 0; i < 60; i++) {
            if (i < 30) {
                studentList.add(new Student("student" + i, 1));
            } else {
                studentList.add(new Student("student" + i, 2));
            }
        }
        Course yuyan = new Course("C语言");
        Course shuxue = new Course("微积分");
        Course yingyu = new Course("大学英语");

        List<Score> yuyanScores = new ArrayList<Score>();
        List<Score> shuxueScores = new ArrayList<Score>();
        List<Score> yingyuScores = new ArrayList<Score>();
        Random rand = new Random();
        for (int i = 0; i < 60; i++) {
            Student stu = studentList.get(i);
            Score yuyanScore = new Score(stu, yuyan, rand.nextInt(100));
            Score shuxueScore = new Score(stu, shuxue, rand.nextInt(100));
            Score yingyuScore = new Score(stu, yingyu, rand.nextInt(100));
            yuyanScores.add(yuyanScore);
            shuxueScores.add(shuxueScore);
            yingyuScores.add(yingyuScore);
        }

        Map<String, List<Score>> scores = new HashMap<String, List<Score>>();
        scores.put("C语言", yuyanScores);
        scores.put("微积分", shuxueScores);
        scores.put("大学英语", yingyuScores);
        return scores;

    }

    public static void main(String[] args) {
        Map<String, List<Score>> scores = new GenScore().InitScore();
        //大学英语成绩单
        List<Score> yingyuScores = scores.get("大学英语");
        for (Score sco: yingyuScores) {
            System.out.println("科目:"+sco.getCourse().getName()+"name:"+sco.getStudent().getName()+",成绩:"+sco.getScore());
        }
        //C语言排序成绩单
        List<Score> yuyanScores = scores.get("C语言");
        System.out.println("排序前");
        for (Score ss: yuyanScores) {
            System.out.println("name:"+ss.getStudent().getName()+",score:"+ss.getScore());
        }
        Collections.sort(yuyanScores);
        System.out.println("排序后");
        for (Score ss: yuyanScores) {
            System.out.println("name:"+ss.getStudent().getName()+",score:"+ss.getScore());
        }
        //随机抽取5个微积分不及格的同学重新考试
        List<Score> shuxueScore = scores.get("微积分");
        List<Score> guakeScore = new ArrayList<>();
        for (Score ss: shuxueScore) {
            if (ss.getScore()<60){
                guakeScore.add(ss);
            }
        }
        for (int i = 0; i < 5; i++) {
            Student stu = guakeScore.get((int) (Math.random()*(guakeScore.size()))).getStudent();
            System.out.println("需要补考的五个学生姓名:"+stu.getName());
        }

    }

}
这是主类及main方法,测试过,貌似可以满足要求。
heanyinqin 2016-10-17
  • 打赏
  • 举报
回复
做出来了,不知道是否能满足你的要求,本人第一次在上面回帖,求高手轻虐!!!
public class Score implements Comparable<Score>{
    private Student student;
    private Course course;
    private double score;


    public Score(Student student, Course course, double score) {
        this.student = student;
        this.course = course;
        this.score = score;
    }

    public Score() {
    }

    @Override
    public String toString() {
        return "Score{" +
                "student=" + student +
                ", course=" + course +
                ", score=" + score +
                '}';
    }

    public Student getStudent() {
        return student;
    }

    public void setStudent(Student student) {
        this.student = student;
    }

    public Course getCourse() {
        return course;
    }

    public void setCourse(Course course) {
        this.course = course;
    }

    public double getScore() {
        return score;
    }

    public void setScore(double score) {
        this.score = score;
    }

    @Override
    public int compareTo(Score o) {
        if (this.getScore()>o.getScore()){
            return -1;
        }else if(this.getScore()<o.getScore()){
            return 1;
        }else {
            return 0;
        }
    }
}
tomorrow_C 2016-10-16
  • 打赏
  • 举报
回复
以下是在main方法里面洗的内容: Map<String, List<Score>> map=GenScore.InitScore(); //1.大学英语成绩单输出 List<Score> yingyuScores=map.get("大学英语"); for(Score s:yingyuScores){ System.out.println(s); } //2.C语言排序成绩单 List<Score> yuyanScores=map.get("C语言"); Collections.sort(yuyanScores); for(Score s:yuyanScores){ System.out.println(s); } //3.随机抽取5个微积分不及格的同学重新考试 List<Score> shuxueScores=map.get("微积分"); System.out.println("微积分5个需要重考的同学:"); List<Score> list=new ArrayList(); for(Score s:shuxueScores){ if(s<60){ list.add(s); } Random r = new Random(); for(int i=0;i<5;i++){ int num=r.nextInt(list.size()) System.out.println(list.get(num).student+":"+list.get(num)); list.remove(num); } } 复制下上面代码到main方法里面试试,

62,625

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧