中易网

设计一个类Student,该类包括姓名,学号和成绩。设计一个方法,按照成绩从高到低顺序输出姓名,学

答案:2  悬赏:40  
解决时间 2021-02-20 01:13
设计一个类Student,该类包括姓名,学号和成绩。设计一个方法,按照成绩从高到低顺序输出姓名,学
最佳答案
public class Student {
    private int id;//学号
    private String name;//姓名
    private int score;//成绩
    public Student(int id, String name, int score) {
        super();
        this.id = id;
        this.name = name;
        this.score = score;
    }
     
    public int getId() {
        return id;
    }
 
    public void setId(int id) {
        this.id = id;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public int getScore() {
        return score;
    }
 
    public void setScore(int score) {
        this.score = score;
    }
 
    @Override
    public String toString() {
        return (id + "   " + name + "   " + score);
    }
     
}
 
 
 
import java.util.Arrays;
 
public class TestStudent {
    static Student[] stus;
    public static void sort(Student[] stus){
        int len = stus.length;//学生个数
        int[] a = new int[len];
        //记录分数
        for (int i = 0; i < len; i++) {
            a[i] = stus[i].getScore();
        }
         
        Arrays.sort(a);//升序排序
         
        for (int i = a.length - 1; i >= 0 ; i--) {
            for (int j = 0; j < stus.length; j++) {
                if(stus[j].getScore() == a[i]){
                    System.out.println(stus[j]);
                }
            }
        }
    }
    public static void main(String[] args) {
        stus = new Student[3];
        stus[0] = new Student(1,"a",87);
        stus[1] = new Student(2,"b",78);
        stus[2] = new Student(3,"c",99);
        System.out.println("按分数从高到低排序:");
        sort(stus);
    }
}按分数从高到低排序:
3   c   99
1   a   87
2   b   78

望采纳哦,亲!

追答:刚才你是不是还问了一道一模一样的题?。。。
全部回答
import java.util.*;
public class Student {
    private String name;
    private int no;
    private double score;
    public Student(String name, int no, double score) {
        this.name = name;
        this.no = no;
        this.score = score;
    }
    public String toString() {
        return String.format("Student[name=%s, no=%d, score=%.2f]", name, no, score);
    }
    public static void main(String[] args) {
        Student s1 = new Student("yugi", 1, 100);
        Student s2 = new Student("kaiba", 2, 90);
        Student s3 = new Student("joey", 3, 98);
        Student[] arr = {
            s1, s2, s3
        };
        System.out.println("排序前:" + Arrays.toString(arr));
        Arrays.sort(arr, new Comparator < Student > () {
            public int compare(Student s1, Student s2) {
                if (s1.score > s2.score) {
                    return -1;
                } else if (s1.score < s2.score) {
                    return 1;
                } else {
                    return 0;
                }
            }
        });
        System.out.println("排序后:" + Arrays.toString(arr));
    }
}
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
张根硕的根很硕大吗?
岳阳楼记通假字
大有生物化工有限公司在哪里啊,我有事要去这
郑州家具哪里款式好看。价钱便宜,质量也不是
前后轮不是一个尺寸的可以吗?
锅底盔地址有知道的么?有点事想过去
全世界含硒元素水源有多少
谁有汝窑釉配方
2013年大版邮票年册多少钱
简单方法推导椭圆面积公式
榆林市城乡建设规划局横山西南新区分局地址在
为什么我的生死狙击中的大杀器图标不见了
跪求高人批八字,指点迷津,1977年农历八月十
上联:大美女,小美女,大小美女吃美食!求下
江西省九江市的对未成年人开放的网吧
推荐资讯
一路有你 票房多少
为什么我买的一对白色的鹦鹉,才喂了一星期就
如何当好同龄人的领导?
有成功脱嘴唇上胡子的嘛,不要推荐 什么 产品
凉拌山菜的做法步骤图,凉拌山菜怎么做好吃
陕西天朔新能源汽车销售服务有限公司在什么地
你还知道哪些是益虫?
老达寨在哪里啊,我有事要去这个地方
娲娲洗发水好不好用
第一题:通过代码完成两个整数内容的交换。第
贵夫人皮草在哪里啊,我有事要去这个地方
12岁男孩对学习还没开窍,不知道怎么去主动学
手机登qq时,显示手机磁盘不足,清理后重新登
刺客的套装怎么选啊?