(JAVA)TreeSet
生活随笔
收集整理的這篇文章主要介紹了
(JAVA)TreeSet
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
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);}}/***** 鍵盤錄入五個學(xué)生信息(姓名,語文成績,數(shù)學(xué)成績,英語成績)按照總分的高低輸出*/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以內(nèi)的隨機數(shù),要求不能重復(fù)*/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;}}}
}
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎
總結(jié)
以上是生活随笔為你收集整理的(JAVA)TreeSet的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 方法论+本土特色,这个BPM平台不简单
- 下一篇: java 入门 博客园_javaweb入