学生成绩的录入排列

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;
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值