黑马程序员技术交流社区

标题: 毕姥爷的视频照着写的就是不行咋办 [打印本页]

作者: wangcongwu    时间: 2014-12-18 01:21
标题: 毕姥爷的视频照着写的就是不行咋办
IO 最后一个视频五个学生输入成绩然后打印到一个文件夹里,我按照毕姥爷的视频做的就是不行
  1. ackage 黑马练习题五个学生三门课排序后放入Txt;

  2. import java.io.*;
  3. import java.util.*;


  4. public  class 键盘输入学生五个成绩三门排序后放文件中 {

  5.         public static void main(String[] args) throws IOException {

  6.                 Set<Student> stus = StudentInfoTool.getStudents();
  7.                 StudentInfoTool.write2File(stus);
  8.                 Iterator<Student> it = stus.iterator();
  9.                 while(it.hasNext())
  10.                 {
  11.                         System.out.println(it.next());
  12.                 }
  13.         }
  14.        
  15. }
  16. class StudentInfoTool
  17. {
  18.        
  19.         public static Set<Student> getStudents() throws IOException
  20.         {
  21.                 BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
  22.                
  23.                 String line= null ;
  24.                 TreeSet<Student> stus = new TreeSet<Student>();
  25.                 while((line = bufr.readLine())!= null  )
  26.                 {
  27.                         if("over".equals(line))
  28.                                 break;
  29.         String[] info = line.split(",");
  30.         Student stu = new Student(info[0],
  31.                                                         Integer.parseInt(info[1]),
  32.                                                         Integer.parseInt(info[2]),
  33.                                                         Integer.parseInt(info[3]));
  34.         stus.add(stu);
  35.                 }
  36.                 bufr.close();
  37.                 return stus;
  38.         }
  39.        
  40.         public static void write2File (Set<Student> stus) throws IOException
  41.         {
  42.                 BufferedWriter bufw = new BufferedWriter(new FileWriter("Student.txt"));
  43.                 for(Student stu : stus )
  44.                 {
  45.                         bufw.write(stu.toString()+"\t");
  46.                         System.out.println("w2f under \t");
  47.                         bufw.write(stu.getSum());
  48.                         System.out.println("fd");
  49.                         bufw.newLine();
  50.                         bufw.flush();
  51.                 }
  52.                 bufw.close();
  53.         }
  54. }


  55. class Student implements Comparable
  56. {
  57.         private String name;
  58.         private int ma,cn,en;
  59.          private int sum;

  60.         Student(String name,int ma,int cn,int en)
  61.         {
  62.                 this.name = name;
  63.                 this.ma = ma;
  64.                 this.en =en;
  65.                 this.cn = cn;
  66.                 sum = ma+cn+en;
  67.         }
  68.        
  69.         public int compareTo(Student s)
  70.         {
  71.                 int num = new Integer(this.sum).compareTo(new Integer(s.sum));
  72.                 if(num == 0)
  73.                         return this.name.compareTo(s.name);
  74.                 return num;
  75.         }
  76.        
  77.         public int compareTo(Object o) {
  78.                 return 0;
  79.         }
  80.         public String getName()
  81.         {
  82.                 return name;
  83.                
  84.         }
  85.         public int getSum()
  86.         {
  87.                 return sum;
  88.         }
  89.         public int hashCode()
  90.         {
  91.                 return name.hashCode()+sum*78;
  92.          }
  93.         public boolean equals(Object obj)
  94.         {
  95.                 if(!(obj instanceof Student))
  96.                         throw new ClassCastException("类型不匹配");
  97.                 Student s = (Student) obj;
  98.                 return this.name.equals(s.name) && this.sum == s.sum;
  99.                
  100.         }
  101.         public String toString()
  102.         {
  103.                 return "student["+name+","+cn+","+en+","+ma+"]";
  104.         }
  105. }       
复制代码

作者: wangcongwu    时间: 2014-12-18 01:23
我去弄出来TreeSet 看也就是第一个学生能够进去,第二个都不行
作者: wangcongwu    时间: 2014-12-18 01:27
网上的别的方法很多,但是我觉得毕姥爷的是最面向对象的感觉,但是就是不知道为啥我这个不行。
作者: wangcongwu    时间: 2014-12-18 03:15
我自己写的Student 类就完全可以比较也能进去二叉树,为啥毕姥爷的不行呢,我把我写的和毕姥爷的放在一起大神们来比较下,难道说我比毕姥爷水平高???
  1. package 黑马练习题五个学生三门课排序后放入Txt;

  2. import java.io.*;
  3. import java.util.*;


  4. public  class 键盘输入学生五个成绩三门排序后放文件中 {

  5.         public static void main(String[] args) throws IOException {

  6.                 Set<Student> stus = StudentInfoTool.getStudents();
  7.                 StudentInfoTool.write2File(stus);
  8.                 Iterator<Student> it = stus.iterator();
  9.                 while(it.hasNext())
  10.                 {
  11.                         System.out.println(it.next());
  12.                 }
  13.         }
  14.        
  15. }
  16. class StudentInfoTool
  17. {
  18.        
  19.         public static Set<Student> getStudents() throws IOException
  20.         {
  21.                 BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
  22.                
  23.                 String line= null ;
  24.                 TreeSet<Student> stus = new TreeSet<Student>();
  25.                 while((line = bufr.readLine())!= null  )
  26.                 {
  27.                         if("over".equals(line))
  28.                                 break;
  29.                         String[] info = line.split(",");
  30.                         for(int i = 0;i<info.length;i++)
  31.                         {
  32.                                 System.out.println("info:"+info[i]);

  33.                         }
  34.                                 Student stu = new Student(info[0],
  35.                                                         Integer.parseInt(info[1]),
  36.                                                         Integer.parseInt(info[2]),
  37.                                                         Integer.parseInt(info[3]));
  38.                                 stus.add(stu);
  39.                 }
  40.                 bufr.close();
  41.                 return stus;
  42.         }
  43.        
  44.         public static void write2File (Set<Student> stus) throws IOException
  45.         {
  46.                 BufferedWriter bufw = new BufferedWriter(new FileWriter("Student.txt"));
  47.                 bufw.write("name"+"\t"+"cn"+"\t"+"en"+"\t"+"ma"+"\t"+"sum");
  48.                 bufw.newLine();
  49.                 for(Student stu : stus )
  50.                 {
  51.                         bufw.write(stu.toString()+"\t");
  52.                         System.out.println("w2f under \t");
  53.                         System.out.println("fd");
  54.                         bufw.newLine();
  55.                         bufw.flush();
  56.                 }
  57.                 bufw.close();
  58.         }
  59. }
  60. //俺自己的
  61. class Student implements Comparable
  62. {
  63.         String name;
  64.         int ma;
  65.         int cn;
  66.         int en;
  67.         int sum;
  68.         Student(String name,int ma,int cn,int en)
  69.         {
  70.                 this.name = name;
  71.                 this.ma = ma;
  72.                 this.cn= cn;
  73.                 this.en=en;
  74.                 this.sum = ma+cn+en;
  75.         }
  76.                 public String getName()
  77.                 {
  78.                         return this.name;
  79.                 }
  80.                 public int getMa()
  81.                 {
  82.                         return this.ma;
  83.                 }
  84.                 public int getSum()
  85.                 {
  86.                         return sum;
  87.                 }
  88.                 public String toString()
  89.                 {
  90.                         return name+"\t"+cn+"\t"+en+"\t"+ma+"\t"+sum;
  91.                 }
  92.          public int compareTo(Object obj) {
  93.                         if(!(obj instanceof Student))
  94.                                 throw new RuntimeException("不是学生对象");
  95.                                 Student s = (Student)obj;
  96.                         //        System.out.println(this.name+"...compare to ..."+s.name);
  97.                                 if(this.sum>s.sum  )
  98.                                      return -1 ;
  99.                         if(this.sum == s.sum  )
  100.                         {       
  101.                                 return this.name.compareTo(s.name);
  102.                         }
  103.                                 return 1;
  104.                 }
  105.                 public boolean equals(Object obj)
  106.                 {
  107.                         if(!(obj instanceof Student))
  108.                                 throw new ClassCastException("类型不匹配");
  109.                         Student s = (Student) obj;
  110.                         return this.name.equals(s.name) && this.sum == s.sum;
  111.                        
  112.                 }
  113. }
  114. //毕姥爷的
  115. //class Student implements Comparable
  116. //{
  117. //        private String name;
  118. //        private int ma,cn,en;
  119. //         private int sum;
  120. //
  121. //        Student(String name,int ma,int cn,int en)
  122. //        {
  123. //                this.name = name;
  124. //                this.ma = ma;
  125. //                this.en =en;
  126. //                this.cn = cn;
  127. //                sum = ma+cn+en;
  128. //        }
  129. //       
  130. //        public int compareTo(Student s)
  131. //        {
  132. //                int num = new Integer(this.sum).compareTo(new Integer(s.sum));
  133. //                if(num == 0)
  134. //                        return this.name.compareTo(s.name);
  135. //                return num;
  136. //        }
  137. //       
  138. //        public int compareTo(Object o) {
  139. //                return 0;
  140. //        }
  141. //        public String getName()
  142. //        {
  143. //                return name;
  144. //               
  145. //        }
  146. //        public int getSum()
  147. //        {
  148. //                return sum;
  149. //        }
  150. //        public int hashCode()
  151. //        {
  152. //                return name.hashCode()+sum*78;
  153. //         }
  154. //        public boolean equals(Object obj)
  155. //        {
  156. //                if(!(obj instanceof Student))
  157. //                        throw new ClassCastException("类型不匹配");
  158. //                Student s = (Student) obj;
  159. //                return this.name.equals(s.name) && this.sum == s.sum;
  160. //               
  161. //        }
  162. //        public String toString()
  163. //        {
  164. //                return "student["+name+","+cn+","+en+","+ma+"]";
  165. //        }
  166. //}       
复制代码





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2