本帖最后由 姚成晖 于 2016-2-17 00:36 编辑
- public class Test {
- public static void main(String[] args) throws IOException {
- Scanner sc = new Scanner(System.in);
-
- TreeSet<Student> ts = new TreeSet<>(new Comparator<Student>(){
- public int compare(Student s1,Student s2){
- int num = s1.getSum()-s2.getSum();
- return num==0?1:num;
- }
- });
- while(ts.size()<5){
- System.out.println("请输入学生及成绩,格式:name,30,30,30");
- String line = sc.nextLine();
- String [] str=line.split(",");
- int chinese = Integer.parseInt(str[1]);
- int math = Integer.parseInt(str[2]);
- int english = Integer.parseInt(str[3]);
- ts.add(new Student(str[0],chinese,math,english));
- }
-
-
- for(Student s:ts){
- System.out.println("姓名"+s.getName()+",语文:"+s.getChinese()+",数学"+s.getMath()+",英语"+s.getEnglish()+",总分"+s.getSum());
- }
- }
- }
- public class Student {
- private String name;
- private int chinese;
- private int math;
- private int english;
-
- Student(){}
-
- Student(String name,int chinese,int math,int english){
- this.name=name;
- this.chinese=chinese;
- this.math=math;
- this.english=english;
- }
-
- public void setName(String name){
- this.name=name;
- }
- public String getName(){
- return name;
- }
-
- public void setChinese(int chinese){
- this.chinese=chinese;
- }
- public int getChinese(){
- return chinese;
- }
- public int getMath() {
- return math;
- }
- public void setMath(int math) {
- this.math = math;
- }
- public int getEnglish() {
- return english;
- }
- public void setEnglish(int english) {
- this.english = english;
- }
-
- public int getSum(){
- return chinese+math+english;
- }
- }
复制代码
|