1. Keyboard Input 5 Student information (name, language score, Math score, English score), according to the total score from high to low output to the console:
Analysis:
A: Define Student Class
B: Create a TreeSet collection
C: How to achieve the total score from high?
D: Keyboard input 5 Student information
E: Traversing the TreeSet collection
2. code example:
(1)Student. Java:
1 Packagecn.itcast_08;2 3 Public classStudent {4 //name5 PrivateString name;6 //Language Achievement7 Private intChinese;8 //Math scores9 Private intMath;Ten //English score One Private int中文版; A - PublicStudent (String name,intChinese,intMathint中文版) { - Super(); the This. Name =name; - This. Chinese =Chinese; - This. Math =Math; - This. 中文版 =中文版; + } - + PublicStudent () { A Super(); at } - - PublicString GetName () { - returnname; - } - in Public voidsetName (String name) { - This. Name =name; to } + - Public intGetchinese () { the returnChinese; * } $ Panax Notoginseng Public voidSetchinese (intChinese) { - This. Chinese =Chinese; the } + A Public intGetmath () { the returnMath; + } - $ Public voidSetmath (intmath) { $ This. Math =Math; - } - the Public intGetenglish () { - return中文版;Wuyi } the - Public voidSetenglish (int中文版) { Wu This. 中文版 =中文版; - } About $ Public intgetsum () { - return This. Chinese + This. Math + This. 中文版; - } -}
(2)Treesetdemo. Java:
1 Packagecn.itcast_08;2 3 ImportJava.util.Comparator;4 ImportJava.util.Scanner;5 ImportJava.util.TreeSet;6 7 /*8 * Keyboard Input 5 Student information (name, language score, Math score, English score), according to the total score from high to low output to the console9 * Ten * Analysis: One * A: Define Student class A * B: Create a TreeSet collection - * C: How to achieve the total score from high? - * D: Keyboard input 5 Student information the * E: Traverse TreeSet Collection - */ - Public classTreesetdemo { - Public Static voidMain (string[] args) { + //Create a TreeSet collection -treeset<student> ts =NewTreeset<student> (NewComparator<student>() { + @Override A Public intCompare (Student s1, Student S2) { at //total score from high to low - intnum = S2.getsum ()-s1.getsum (); - //the same total score is not necessarily the same language - intnum2 = num = = 0? S1.getchinese ()-S2.getchinese (): num; - //the same score is not necessarily the same math - intnum3 = Num2 = = 0? S1.getmath ()-S2.getmath (): num2; in //the same total score is not necessarily the same English - intNUM4 = Num3 = = 0? S1.getenglish ()-s2.getenglish (): num3; to //names aren't necessarily the same. + intNUM5 = Num4 = = 0?s1.getname (). CompareTo (S2.getname ()) - : num4; the returnNUM5; * } $ });Panax Notoginseng -System.out.println ("Start of Student information entry"); the //Keyboard Input 5 Student information + for(intx = 1; x <= 5; X + +) { AScanner sc =NewScanner (system.in); theSystem.out.println ("Please enter the" + x + "Student's name:"); +String name =sc.nextline (); -System.out.println ("Please enter the" + x + "Student's language score:"); $String chinesestring =sc.nextline (); $System.out.println ("Please enter" + x + "Student's math Score:"); -String mathstring =sc.nextline (); -System.out.println ("Please enter the" + x + "student's English Score:"); theString englishstring =sc.nextline (); - Wuyi //encapsulate data into student objects theStudent s =NewStudent (); - s.setname (name); Wu S.setchinese (Integer.parseint (chinesestring)); - S.setmath (Integer.parseint (mathstring)); About s.setenglish (Integer.parseint (englishstring)); $ - //add student objects to the collection - Ts.add (s); - } ASYSTEM.OUT.PRINTLN ("Student Information Entry Complete"); + theSystem.out.println ("The learning information is sorted from high to low as follows:"); -System.out.println ("name \ t Chinese result \ t Math score \ t English score"); $ //iterating through the collection the for(Student s:ts) { theSystem.out.println (S.getname () + "\ T" + s.getchinese () + "\ T" the+ S.getmath () + "\ T" +s.getenglish ()); the } - } in}
Run the effect as follows:
A collection framework for Java Fundamentals Enhancement Note 49: Keyboard input 5 Student information (name, language score, Math score, English score), according to the total score from high to low output to the console