我自己写的Student 类就完全可以比较也能进去二叉树,为啥毕姥爷的不行呢,我把我写的和毕姥爷的放在一起大神们来比较下,难道说我比毕姥爷水平高???
- package 黑马练习题五个学生三门课排序后放入Txt;
- import java.io.*;
- import java.util.*;
- public class 键盘输入学生五个成绩三门排序后放文件中 {
- public static void main(String[] args) throws IOException {
- Set<Student> stus = StudentInfoTool.getStudents();
- StudentInfoTool.write2File(stus);
- Iterator<Student> it = stus.iterator();
- while(it.hasNext())
- {
- System.out.println(it.next());
- }
- }
-
- }
- class StudentInfoTool
- {
-
- public static Set<Student> getStudents() throws IOException
- {
- BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
-
- String line= null ;
- TreeSet<Student> stus = new TreeSet<Student>();
- while((line = bufr.readLine())!= null )
- {
- if("over".equals(line))
- break;
- String[] info = line.split(",");
- for(int i = 0;i<info.length;i++)
- {
- System.out.println("info:"+info[i]);
- }
- Student stu = new Student(info[0],
- Integer.parseInt(info[1]),
- Integer.parseInt(info[2]),
- Integer.parseInt(info[3]));
- stus.add(stu);
- }
- bufr.close();
- return stus;
- }
-
- public static void write2File (Set<Student> stus) throws IOException
- {
- BufferedWriter bufw = new BufferedWriter(new FileWriter("Student.txt"));
- bufw.write("name"+"\t"+"cn"+"\t"+"en"+"\t"+"ma"+"\t"+"sum");
- bufw.newLine();
- for(Student stu : stus )
- {
- bufw.write(stu.toString()+"\t");
- System.out.println("w2f under \t");
- System.out.println("fd");
- bufw.newLine();
- bufw.flush();
- }
- bufw.close();
- }
- }
- //俺自己的
- class Student implements Comparable
- {
- String name;
- int ma;
- int cn;
- int en;
- int sum;
- Student(String name,int ma,int cn,int en)
- {
- this.name = name;
- this.ma = ma;
- this.cn= cn;
- this.en=en;
- this.sum = ma+cn+en;
- }
- public String getName()
- {
- return this.name;
- }
- public int getMa()
- {
- return this.ma;
- }
- public int getSum()
- {
- return sum;
- }
- public String toString()
- {
- return name+"\t"+cn+"\t"+en+"\t"+ma+"\t"+sum;
- }
- public int compareTo(Object obj) {
- if(!(obj instanceof Student))
- throw new RuntimeException("不是学生对象");
- Student s = (Student)obj;
- // System.out.println(this.name+"...compare to ..."+s.name);
- if(this.sum>s.sum )
- return -1 ;
- if(this.sum == s.sum )
- {
- return this.name.compareTo(s.name);
- }
- return 1;
- }
- public boolean equals(Object obj)
- {
- if(!(obj instanceof Student))
- throw new ClassCastException("类型不匹配");
- Student s = (Student) obj;
- return this.name.equals(s.name) && this.sum == s.sum;
-
- }
- }
- //毕姥爷的
- //class Student implements Comparable
- //{
- // private String name;
- // private int ma,cn,en;
- // private int sum;
- //
- // Student(String name,int ma,int cn,int en)
- // {
- // this.name = name;
- // this.ma = ma;
- // this.en =en;
- // this.cn = cn;
- // sum = ma+cn+en;
- // }
- //
- // public int compareTo(Student s)
- // {
- // int num = new Integer(this.sum).compareTo(new Integer(s.sum));
- // if(num == 0)
- // return this.name.compareTo(s.name);
- // return num;
- // }
- //
- // public int compareTo(Object o) {
- // return 0;
- // }
- // public String getName()
- // {
- // return name;
- //
- // }
- // public int getSum()
- // {
- // return sum;
- // }
- // public int hashCode()
- // {
- // return name.hashCode()+sum*78;
- // }
- // public boolean equals(Object obj)
- // {
- // if(!(obj instanceof Student))
- // throw new ClassCastException("类型不匹配");
- // Student s = (Student) obj;
- // return this.name.equals(s.name) && this.sum == s.sum;
- //
- // }
- // public String toString()
- // {
- // return "student["+name+","+cn+","+en+","+ma+"]";
- // }
- //}
复制代码 |