类训练-学生管理实现

建立一个学生类(姓名,学号,3门课成绩(英语,数学,语文),总分),类的方法:输入,输出,学生数据,根据总分排序,打印学生名次)

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("请输入你要录入的学生人数:");
        int studentNum = in.nextInt();

        System.out.println("===========================================================");

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

        for(int i=0;i<studentNum;i++) {
            System.out.println("输入学生的学号:");
            String studentID = in.next();

            System.out.println("输入学生的姓名:");
            String studentName = in.next();

            System.out.println("输入数学科目的成绩:");
            int math = in.nextInt();

            System.out.println("输入英语科目的成绩:");
            int English = in.nextInt();

            System.out.println("输入语文科目的成绩:");
            int chinese = in.nextInt();

            Course course = new Course(math,English,chinese);
            Student student = new Student(studentID,studentName,course);
            list.add(student);
            System.out.println("===========================================================");
        }

        //排序
        list.sort(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                if(o1.getCourse().getSum()>o2.getCourse().getSum())
                    return -1;
                else
                    return 1;
            }

        });

        //遍历输出
        for(Student student : list) {
            System.out.println("学号:"+student.getStudentID()+"\t"+"姓名:"+student.getStudentName()+"\t"
                    + "总分:"+student.getCourse().getSum());
        }

    }

}

/**
 * @描述 学生类
 * @author iceyu
 *
 */
class Student{
    private String studentID;
    private String studentName;
    private Course course;

    public Student() {}
    public Student(String studentID,String studentName,Course course) {
        this.course = course;
        this.studentID = studentID;
        this.studentName = studentName;
    }

    public String getStudentID() {
        return studentID;
    }
    public void setStudentID(String studentID) {
        this.studentID = studentID;
    }
    public String getStudentName() {
        return studentName;
    }
    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }
    public Course getCourse() {
        return course;
    }
    public void setCourse(Course course) {
        this.course = course;
    }

}

/**
 * @描述 课程类
 * @author iceyu
 *
 */
class Course{
    private int math;
    private int English;
    private int chinese;
    private int sum;
    public Course() {}
    public Course(int math,int English,int chinese) {
        this.math = math;
        this.chinese = chinese;
        this.English = English;
    }

    public int getSum() {
        return this.chinese + this.English + this.math;
    }
    public void setSum(int sum) {
        this.sum = sum;
    }
    public int getMath() {
        return math;
    }
    public void setMath(int math) {
        this.math = math;
    }
    public int getEnglish() {
        return English;
    }
    public void setEnglish(int english) {
        English = english;
    }
    public int getChinese() {
        return chinese;
    }
    public void setChinese(int chinese) {
        this.chinese = chinese;
    }

}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142

最终结果:

请输入你要录入的学生人数:
3
===========================================================
输入学生的学号:
001
输入学生的姓名:
小明
输入数学科目的成绩:
67
输入英语科目的成绩:
78
输入语文科目的成绩:
55
===========================================================
输入学生的学号:
002
输入学生的姓名:
小红
输入数学科目的成绩:
89
输入英语科目的成绩:
99
输入语文科目的成绩:
90
===========================================================
输入学生的学号:
003
输入学生的姓名:
小白
输入数学科目的成绩:
78
输入英语科目的成绩:
67
输入语文科目的成绩:
55
===========================================================
最终排名:
学号:002  姓名:小红   总分:278
学号:001  姓名:小明   总分:200
学号:003  姓名:小白   总分:200
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40

覆盖tostring方法输出:

package 学生管理;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("请输入你要录入的学生人数:");
        int studentNum = in.nextInt();

        System.out.println("===========================================================");

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

        for(int i=0;i<studentNum;i++) {
            System.out.println("输入学生的学号:");
            String studentID = in.next();

            System.out.println("输入学生的姓名:");
            String studentName = in.next();

            System.out.println("输入数学科目的成绩:");
            int math = in.nextInt();

            System.out.println("输入英语科目的成绩:");
            int English = in.nextInt();

            System.out.println("输入语文科目的成绩:");
            int chinese = in.nextInt();

            Course course = new Course(math,English,chinese);
            Student student = new Student(studentID,studentName,course);
            list.add(student);
            System.out.println("===========================================================");
        }

        //排序
        list.sort(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                if(o1.getCourse().getSum()>o2.getCourse().getSum())
                    return -1;
                else
                    return 1;
            }

        });

        System.out.println("最终排名:");
        //遍历输出
        for(Student student : list) {
//          System.out.println("学号:"+student.getStudentID()+"\t"+"姓名:"+student.getStudentName()+"\t"
//                  + "总分:"+student.getCourse().getSum());
            System.out.println(student.toString());
        }

    }

}

/**
 * @描述 学生类
 * @author iceyu
 *
 */
class Student{
    private String studentID;
    private String studentName;
    private Course course;

    public Student() {}
    public Student(String studentID,String studentName,Course course) {
        this.course = course;
        this.studentID = studentID;
        this.studentName = studentName;
    }

    public String getStudentID() {
        return studentID;
    }
    public void setStudentID(String studentID) {
        this.studentID = studentID;
    }
    public String getStudentName() {
        return studentName;
    }
    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }
    public Course getCourse() {
        return course;
    }
    public void setCourse(Course course) {
        this.course = course;
    }

    //重写类的ToString方法
    @Override
    public String toString() {
        String result = "学号:"+this.getStudentID()+"\t"+"姓名:"+this.getStudentName()+"\t"
                + "总分:"+this.getCourse().getSum();
        return result;
    }


}

/**
 * @描述 课程类
 * @author iceyu
 *
 */
class Course{
    private int math;
    private int English;
    private int chinese;
    private int sum;
    public Course() {}
    public Course(int math,int English,int chinese) {
        this.math = math;
        this.chinese = chinese;
        this.English = English;
    }

    public int getSum() {
        return this.chinese + this.English + this.math;
    }
    public void setSum(int sum) {
        this.sum = sum;
    }
    public int getMath() {
        return math;
    }
    public void setMath(int math) {
        this.math = math;
    }
    public int getEnglish() {
        return English;
    }
    public void setEnglish(int english) {
        English = english;
    }
    public int getChinese() {
        return chinese;
    }
    public void setChinese(int chinese) {
        this.chinese = chinese;
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值