Demand:
Keyboard Input 5 Student information (name, language score, Math score, English score), according to the total score from high to low deposit text file
Analysis:
A: Create Student class
B: Create a Collection Object
Treeset<student>
C: Keyboard entry Student Information store to collection
D: Iterate through the collection and write the data to a text file
First create a student class
1 Packagezl_test;2 /**3 * It's a record of students ' grades4 * @authorLZL5 *6 */7 Public classStudent {8 PrivateString name;9 Private intChinese;Ten Private intMath; One Private int中文版; A - - PublicStudent () { the Super(); - //TODO auto-generated Constructor stub - } - + - PublicStudent (String name,intChinese,intMathint中文版) { + Super(); A This. Name =name; at This. Chinese =Chinese; - This. Math =Math; - This. 中文版 =中文版; - } - - in PublicString GetName () { - returnname; to } + - the Public voidsetName (String name) { * This. Name =name; $ }Panax Notoginseng - the Public intGetchinese () { + returnChinese; A } the + - Public voidSetchinese (intChinese) { $ This. Chinese =Chinese; $ } - - the Public intGetmath () { - returnMath;Wuyi } the - Wu Public voidSetmath (intmath) { - This. Math =Math; About } $ - - Public intGetenglish () { - return中文版; A } + the - Public voidSetenglish (int中文版) { $ This. 中文版 =中文版; the } the the Public intgetsum () { the return This. Chinese + This. 中文版 + This. Math; - in the } the About}
View Code
Specific implementation classes:
1 Packagezl_test;2 3 ImportJava.io.BufferedWriter;4 ImportJava.io.FileWriter;5 Importjava.io.IOException;6 ImportJava.util.Comparator;7 ImportJava.util.Scanner;8 ImportJava.util.TreeSet;9 Ten Public classStudentdemo { One A Public Static voidMain (string[] args)throwsIOException { - //To create a collection object -treeset<student> ts =NewTreeset<student> (NewComparator<student>() { the //Comparator - Public intCompare (Student s1, Student S2) { - - intNUM1 = S2.getsum ()-s1.getsum (); + intnum2 = NUM1 = = 0? S2.getchinese ()-S1.getchinese (): NUM1; - intnum3 = Num2 = = 0? S2.getmath ()-S1.getmath (): num2; + intNUM4 = Num3 = = 0?s2.getname (). CompareTo (S1.getname ()) A : num3; at returnnum4; - - } - }); - - //Keyboard entry Student Information in for(intx = 0; x < 5; X + +) { -Scanner sc =NewScanner (system.in); to +System.out.println ("Please enter student's name:"); -String name =sc.nextline (); theSYSTEM.OUT.PRINTLN ("Please input language score"); * intChinese =sc.nextint (); $System.out.println ("Please enter math score");Panax Notoginseng intMath =sc.nextint (); -SYSTEM.OUT.PRINTLN ("Please input English score"); the int中文版 =sc.nextint (); + A //create student object, call student class theStudent s =NewStudent (); + s.setname (name); - S.setchinese (Chinese); $ S.setmath (math); $ s.setenglish (中文版); - - //add information to the collection the Ts.add (s); - }Wuyi the //text Open information that can be read, with buffer character stream - //To create a buffered character output stream object WuBufferedWriter BW =NewBufferedWriter (NewFileWriter ("Student.txt")); - //write the contents of the text first AboutBw.write ("Student performance record"); $ bw.newline (); - Bw.flush (); -Bw.write ("Language score" + "T" + "Math Score" + "T" + "English Score"); - bw.newline (); A Bw.flush (); + the //iterate through the collection and add the resulting data to the file - for(Student student:ts) { $ //stitching on the information obtained theStringBuilder SB =NewStringBuilder (); theSb.append (Student.getname () + "\t\t") the. Append (Student.getchinese () + "\t\t") the. Append (Student.getmath () + "\t\t") - . Append (Student.getenglish ()); in Bw.write (sb.tostring ()); the bw.newline (); the } About bw.close (); theSYSTEM.OUT.PRINTLN ("Student scores are stored"); the } the}
Io Stream Practice 4--Keyboard Input student performance information, sorted into text