您的位置:首页 > 其它

学生成绩的录入排列

2013-12-02 16:28 190 查看
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;

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: