package com.doit.day25;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
public class PaiXuDemo12 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ArrayList<Student52> al = new ArrayList<Student52>();
for (int i = 1; i < 4; i++) {
System.out.println("请输入第" + i + "个学生的姓名");
String name = sc.next();
System.out.println("请输入第" + i + "个学生的学号");
int id = sc.nextInt();
System.out.println("请输入第" + i + "个学生的语文成绩");
int chinese = sc.nextInt();
System.out.println("请输入第" + i + "个学生的历史成绩");
int history = sc.nextInt();
System.out.println("请输入第" + i + "个学生的数学成绩");
int math = sc.nextInt();
Student52 stu = new Student52(name, id, chinese, math, history);
al.add(stu);
}
System.out.println(al);
Collections.sort(al,new Comparator<Student52>(){
@Override
public int compare(Student52 o1, Student52 o2) {
/*if(o2.getScore() == o1.getScore()){
return o1.name.compareTo(o2.name);
}else if(o1.name.equals(o2.name)){
return o1.id - o2.id;
}
return o2.getScore() - o1.getScore();*/
int i = o2.getScore() - o1.getScore();
int i1 = i == 0 ? o1.name.compareTo(o2.name) : i;
int i2 = i1 == 0 ? o1.id - o2.id : i1;
return i2;
}
});
System.out.println(al);
}
}
class Student52{
String name;
int id;
int chinese;
int history;
int math;
public Student52() {
super();
// TODO Auto-generated constructor stub
}
public Student52(String name, int id, int chinese, int math, int history) {
super();
this.name = name;
this.id = id;
this.chinese = chinese;
this.math = math;
this.history = history;
}
@Override
public String toString() {
return "Student52 [name=" + name + ", id=" + id + ", chinese=" + chinese + ", math=" + math + ", history="
+ history + "]";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getChinese() {
return chinese;
}
public void setChinese(int chinese) {
this.chinese = chinese;
}
public int getMath() {
return math;
}
public void setMath(int math) {
this.math = math;
}
public int getHistory() {
return history;
}
public void setHistory(int history) {
this.history = history;
}
public int getScore(){
return this.chinese + this.history + this.math;
}
}
排序
最新推荐文章于 2024-04-21 17:41:25 发布