问题:如何直接将手动输入的学生信息(如下所示=“ Elise”)与其他学生Aggergate标记进行比较,然后对输出文件顶部具有最大“ Aggergate”编号的数据进行排序。
public class Student2 implements Comparable<Student2> {
@Override
public int compareTo(Student2 o) {
return Double.valueOf(this.aggerate()).compareTo(Double.valueOf(o.aggerate()));
if (result > 0) {
return 1;
} else if (result < 0) {
return -1;
} else {
return 0;
}
}
public static void main(String... args) {
Set<Student2> Stu = new TreeSet<>(
Comparator.comparing(Student2::aggregate).reversed()
.andThen(Student2::getId));
Stu.add(new Student2(25321, "Elsie", 51.5, 45.6, 48.5, "3rd", "Proceed To Stage 2"));
Stu.stream().forEach((c) -> {
System.out.println(c);
});
}
public double aggerate() {
DecimalFormat decFormat = new DecimalFormat("#.#");
double aggerateMarks = Double.valueOf(decFormat.format((IR101Grades + IR102Grades) / 2));
aggerateMarks = Math.round(aggerateMarks * 100.0) / 100.0;
return aggerateMarks;
}
public String toString() {
return "\n" + studentID + " |" + studentName + "\n" + "IR101: " + IR101Grades + " | " + "IR102: " + IR102Grades + " |"
+ " Aggregate Mark: " + aggerate() + "\n" + "Class: " + setGrade() + " | " + "Outcome: " + setClass()
+ "\n" + "\n" + "-----------------------------------------------------" + "\n" + "\n";
}
最佳答案
要创建一个按集合反向排序的TreeSet,您可以执行
Set<Student2> Stu = new TreeSet<>(
Comparator.comparing(Student2::aggregate).reversed()
.andThen(Student2::getId));
这样,您的树将以相反的顺序排列。
注意:以防万一两个学生的聚集总数相同,getID应该确保它们永远不会被视为重复