题目:
按照总分从高到低输中到控制台
如果总分一样,按照语文成绩排序
如果语文成绩一样,按照数学成绩排序
如果数学成绩一样,按照英语成绩排序
如果英文成绩一样,按照年龄来排序
如果年龄一样,按照姓名的字母顺序排序
代码:
public class Student implements Comparable<Student>{
private String name;
private int age;
private int Chinese;
private int mathematics;
private int English;
public Student() {
}
public Student(String name, int age, int Chinese, int mathematics, int English) {
this.name = name;
this.age = age;
this.Chinese = Chinese;
this.mathematics = mathematics;
this.English = English;
}
/**
* 获取
* @return name
*/
public String getName() {
return name;
}
/**
* 设置
* @param name
*/
public void setName(String name) {
this.name = name;
}
/**
* 获取
* @return age
*/
public int getAge() {
return age;
}
/**
* 设置
* @param age
*/
public void setAge(int age) {
this.age = age;
}
/**
* 获取
* @return Chinese
*/
public int getChinese() {
return Chinese;
}
/**
* 设置
* @param Chinese
*/
public void setChinese(int Chinese) {
this.Chinese = Chinese;
}
/**
* 获取
* @return mathematics
*/
public int getMathematics() {
return mathematics;
}
/**
* 设置
* @param mathematics
*/
public void setMathematics(int mathematics) {
this.mathematics = mathematics;
}
/**
* 获取
* @return English
*/
public int getEnglish() {
return English;
}
/**
* 设置
* @param English
*/
public void setEnglish(int English) {
this.English = English;
}
public String toString() {
int sum=this.Chinese+this.mathematics+this.English;
return "Student{name = " + name + ", age = " + age + ", Chinese = " + Chinese + ", mathematics = " + mathematics + ", English = " + English + ",SUM = "+sum+"}";
}
/**
* Compares this object with the specified object for order. Returns a
* negative integer, zero, or a positive integer as this object is less
* than, equal to, or greater than the specified object.
*
* <p>The implementor must ensure <tt>sgn(x.compareTo(y)) ==
* -sgn(y.compareTo(x))</tt> for all <tt>x</tt> and <tt>y</tt>. (This
* implies that <tt>x.compareTo(y)</tt> must throw an exception iff
* <tt>y.compareTo(x)</tt> throws an exception.)
*
* <p>The implementor must also ensure that the relation is transitive:
* <tt>(x.compareTo(y)>0 && y.compareTo(z)>0)</tt> implies
* <tt>x.compareTo(z)>0</tt>.
*
* <p>Finally, the implementor must ensure that <tt>x.compareTo(y)==0</tt>
* implies that <tt>sgn(x.compareTo(z)) == sgn(y.compareTo(z))</tt>, for
* all <tt>z</tt>.
*
* <p>It is strongly recommended, but <i>not</i> strictly required that
* <tt>(x.compareTo(y)==0) == (x.equals(y))</tt>. Generally speaking, any
* class that implements the <tt>Comparable</tt> interface and violates
* this condition should clearly indicate this fact. The recommended
* language is "Note: this class has a natural ordering that is
* inconsistent with equals."
*
* <p>In the foregoing description, the notation
* <tt>sgn(</tt><i>expression</i><tt>)</tt> designates the mathematical
* <i>signum</i> function, which is defined to return one of <tt>-1</tt>,
* <tt>0</tt>, or <tt>1</tt> according to whether the value of
* <i>expression</i> is negative, zero or positive.
*
* @param o the object to be compared.
* @return a negative integer, zero, or a positive integer as this object
* is less than, equal to, or greater than the specified object.
* @throws NullPointerException if the specified object is null
* @throws ClassCastException if the specified object's type prevents it
* from being compared to this object.
*/
@Override
public int compareTo(Student o) {
int sum=this.getChinese()+this.getMathematics()+this.getEnglish();
int sum2=o.getChinese()+o.getMathematics()+o.getEnglish();
//总分
int i = sum - sum2;
//总分相同 语文比较
i = i == 0 ? this.getChinese() - o.getChinese() : i;
//语文相同 数学比较
i=i==0?this.getMathematics()-o.getMathematics():i;
//数学相同 英语比较M
i=i==0?this.getEnglish()-o.getEnglish():i;
//英语相同 年龄比较
i=i==0?this.getAge()-o.getAge():i;
//年龄相同 姓名字母比较
i=i==0?this.getName().compareTo(o.getName()):i;
return i;
}
}
测试类:
import java.util.TreeSet;
public class Test {
public static void main(String[] args) {
TreeSet<Student> st = new TreeSet<>();
Student s1 = new Student("xiaohang", 14, 60, 50, 60);
Student s2 = new Student("xiaozhang", 14, 70, 100, 90);
Student s3 = new Student("xiangyv", 14, 20, 80, 70);
Student s4 = new Student("licheng", 14, 70, 50, 60);
Student s5 = new Student("aini", 14, 30, 50, 90);
st.add(s1);
st.add(s2);
st.add(s3);
st.add(s4);
st.add(s5);
for (Student student : st) {
System.out.println(student);
}
}
}
结果:
Student{name = xiangyv, age = 14, Chinese = 20, mathematics = 80, English = 70,SUM = 170}
Student{name = aini, age = 14, Chinese = 30, mathematics = 50, English = 90,SUM = 170}
Student{name = xiaohang, age = 14, Chinese = 60, mathematics = 50, English = 60,SUM = 170}
Student{name = licheng, age = 14, Chinese = 70, mathematics = 50, English = 60,SUM = 180}
Student{name = xiaozhang, age = 14, Chinese = 70, mathematics = 100, English = 90,SUM = 260}