关闭

学生成绩的录入排列

450人阅读 评论(0) 收藏 举报
分类:

1. 学生类(学生编号、学生名、班级、成绩、年龄)
实现比较接口

输入5个学生的信息,

使用Arrays.sort()将这个5个学生按学生成绩从低到高排列

学生比较规则
:
1、成绩比较
|_成绩相等比较学号(学号越小就越排前面)


import java.util.Arrays;


public class Test {


public static void main(String[] args) {
Student []array= new Student[]{
new Student(1,"小王","二年级",56,5),
new Student(2,"小李","二年级",57,5),
new Student(3,"小张","二年级",76,5),
new Student(4,"小孙","二年级",47,5)
};
Arrays.sort(array);
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
}

}

import java.util.Arrays;
public class Student implements Comparable {
public int id;
public String name;
public String grade;
public double score;
public int age;
public Student(int id, String name, String grade, double score, int age) {
super();
this.id = id;
this.name = name;
this.grade = grade;
this.score = score;
this.age = age;
}
@Override
public int compareTo(Object o) {
Student student=(Student)o;
if(this.score>student.score){
return 1;
}
else{ if(this.score
return -1;
}

else if(this.id
return 1;
}
else if(this.id
return -1;
}
return 0;
}

}
public String toString (){
String str=this.id+" "+this.name+" "+this.score+" "+this.grade+" "+this.age;
return str;
}
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:22985次
    • 积分:717
    • 等级:
    • 排名:千里之外
    • 原创:46篇
    • 转载:36篇
    • 译文:0篇
    • 评论:3条
    文章分类
    最新评论