一到java题,定义一个类,,该类包括姓名,学号和成绩。设计一个方法

2020-09-09 教育 86阅读
/**
 * Created by itjob 深圳远标培训 on 16/9/29.
 */
public class Student {
    private String name;//姓名
    private String stuNo;//学号
    private Float score;//成绩
    /**
     * 排序后显示学生信息
     * @param stus
     */
    public void showSortInfo(Student [] stus){
        for (int i = 0; i < stus.length; i++) {
            for (int j = 0; j < stus.length - i -1; j++) {
                if(stus[j].getScore() < stus[j+1].getScore()){
                    Student stu = stus[j];
                    stus[j] = stus[j+1];
                    stus[j+1] = stu;
                }
            }
        }
        //显示暑促
        for (Student s :
                stus) {
            System.out.println(s);
        }
    }
    //main 函数测试
    public static void main(String[] args) {
        Student [] stus = new Student[]{
                new Student("a","3",80f),
                new Student("a2","32",70f),
                new Student("a3","33",60f),
                new Student("a5","35",90f),
                new Student("a6","36",81f),
        };
        new Student().showSortInfo(stus);
    }
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", stuNo='" + stuNo + '\'' +
                ", score=" + score +
                '}';
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getStuNo() {
        return stuNo;
    }
    public void setStuNo(String stuNo) {
        this.stuNo = stuNo;
    }
    public Float getScore() {
        return score;
    }
    public void setScore(Float score) {
        this.score = score;
    }
    public Student() {
        super();
    }
    public Student(String name, String stuNo, Float score) {
        this.name = name;
        this.stuNo = stuNo;
        this.score = score;
    }
}

结果

Student{name='a5', stuNo='35', score=90.0}

Student{name='a6', stuNo='36', score=81.0}

Student{name='a', stuNo='3', score=80.0}

Student{name='a2', stuNo='32', score=70.0}

Student{name='a3', stuNo='33', score=60.0}

声明:你问我答网所有作品(图文、音视频)均由用户自行上传分享,仅供网友学习交流。若您的权利被侵害,请联系fangmu6661024@163.com