package homework;/*** @author Alina* @date 2021年09月22日 10:34 下午*/
class SetTestStudent implements Comparable<SetTestStudent>{private String name;private int gradeScoresOfChinese;private int gradeScoresOfMath;private int gradeScoresOfEnglish;public int TotalScore(){return this.gradeScoresOfChinese+this.gradeScoresOfMath+this.gradeScoresOfEnglish;}public int compareTo(SetTestStudent s){int Score = s.TotalScore() - this.TotalScore();return Score==0? this.getName().compareTo(s.getName()):Score;}public String toString(){return "name : "+getName()+" "+"chinese: "+getGradeScoresOfChinese()+" "+"math: "+getGradeScoresOfMath()+ " "+"english: "+getGradeScoresOfEnglish();}public SetTestStudent(String name, int gradeScoresOfChinese, int gradeScoresOfMath, int gradeScoresOfEnglish) {this.name = name;this.gradeScoresOfChinese = gradeScoresOfChinese;this.gradeScoresOfMath = gradeScoresOfMath;this.gradeScoresOfEnglish = gradeScoresOfEnglish;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getGradeScoresOfChinese() {return gradeScoresOfChinese;}public void setGradeScoresOfChinese(int gradeScoresOfChinese) {this.gradeScoresOfChinese = gradeScoresOfChinese;}public int getGradeScoresOfMath() {return gradeScoresOfMath;}public void setGradeScoresOfMath(int gradeScoresOfMath) {this.gradeScoresOfMath = gradeScoresOfMath;}public int getGradeScoresOfEnglish() {return gradeScoresOfEnglish;}public void setGradeScoresOfEnglish(int gradeScoresOfEnglish) {this.gradeScoresOfEnglish = gradeScoresOfEnglish;}
}
package homework;import java.util.*;/*** @author Alina* @date 2021年09月22日 10:21 上午**/
public class SetTest {public static void main(String[] args) {method_3();}public static void method_3(){Scanner sc = new Scanner(System.in);TreeSet< SetTestStudent> t = new TreeSet<>();for (int x= 0;x<5;x++){String StudentInformation = sc.nextLine();String[]studentInformation = StudentInformation.split(" +");t.add(new SetTestStudent(studentInformation[0],Integer.parseInt(studentInformation[1]),Integer.parseInt(studentInformation[2]),Integer.parseInt(studentInformation[3])));}for(SetTestStudent s :t ){System.out.println(s);}}/***** 键盘录入五个学生信息(姓名,语文成绩,数学成绩,英语成绩)按照总分的高低输出*/public static void method_2() {TreeSet<SetTestStudent> stu = new TreeSet<>();for (int x = 0; x < 5; x++) {System.out.println("please input your name:");String StudentName = (new Scanner(System.in)).nextLine();System.out.println("Please enter your Chinese score:");int ScoresOfChinese = (new Scanner(System.in)).nextInt();System.out.println("Please enter your Math score:");int ScoresOfMath = (new Scanner(System.in)).nextInt();System.out.println("Please enter your English score:");int ScoresOfEnglish = (new Scanner(System.in)).nextInt();SetTestStudent stfg = new SetTestStudent(StudentName, ScoresOfChinese, ScoresOfMath, ScoresOfEnglish);stu.add(stfg);}Iterator it = stu.iterator();while (it.hasNext()) {System.out.println(it.next());}}/**** @author Alina* @date 2021/9/22 10:30 下午* 生成10个20以内的随机数,要求不能重复*/public static void method_1(){TreeSet<Integer> t = new TreeSet<>();Random r = new Random();while (true) {if (t.size() <= 10) {int num = r.nextInt(100) + 1;t.add(num);} else {System.out.println(t);break;}}}
}