Practice:
Requirements: There are five students, each student has 3 courses of results,
Enter the above data from the keyboard (including the name, the results of each class),
Input format: such as: zhagnsan,30,40,60 calculated the total,
And the student's information and calculated total score high and low order in the disk file "Stud.txt".
1, describe the student object.
2, defines a tool class that can manipulate student objects.
Thought:
1, by getting the keyboard input a row of data, and the information in the row is taken out to encapsulate the student object.
2, because students have a lot, then you need to store, use to the collection. Because the student's total score is to be sorted.
So you can use TreeSet.
3, writes the information of the collection to a file.
Importjava.io.*;ImportJava.util.*;class Studenttest { Public Static void Main(string[] args)throwsIOException {set<student> students=studentinfoutil.getstudents (); Studentinfoutil.write2file (students); }}class Student implements comparable{ PublicString name; Public intyu,shu,ying; Public intZong Student (String name,intYuintShuintYing) { This. name=name; This. Yu=yu; This. Shu=shu; This. ying=ying; This. zong=yu+shu+ying; } Public int CompareTo(Object obj) {if(! (objinstanceofStudent))Throw NewRuntimeException ("not a student."); Student stu= (Student) obj;if( This. Zong<stu.zong)return 1;Else if( This. Zong==stu.zong)return This. Name.compareto (Stu.name);return-1; }}class studentinfoutil{ Public StaticSet<student>getstudents()throwsIOException {BufferedReader bur=NewBufferedReader (NewInputStreamReader (system.in)); String line=NULL; Set<student> students=NewTreeset<student> (); while((Line=bur.readline ())! =NULL) {if(Line.equals ("Over")) Break; String[] Info=line.split (","); Student stu=NewStudent (info[0],integer.parseint (info[1]), Integer.parseint (info[2]), Integer.parseint (info[3])); Students.add (Stu); } bur.close ();returnStudents } Public Static void Write2file(set<student> students)throwsIOException {BufferedWriter buw=NewBufferedWriter (NewFileWriter ("Stuinfo.txt")); for(Student stu:students) {Buw.write (stu.name+","+stu.zong); Buw.newline (); Buw.flush (); } buw.close (); }}
Java Fundamentals IO Stream (practice)