A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

  1. /*
  2. 有五个学生,每个学生有三门课的成绩,
  3. 从键盘输入以上数据(包括姓名,三门课的成绩),
  4. 输入的格式,如;zhangsan,30,40,60,计算出总成绩,
  5. 并把学生的信息和计算出的总分数高低顺序存放在磁盘文件“stud.txt”中
  6. */
  7. import java.io.*;
  8. import java.util.*;
  9. class Student implements Comparable<Student>
  10. {
  11.         private String name;
  12.         private int zongfen;
  13.         private int en,end,end1;
  14.         Student(String name,int en,int end ,int end1)
  15.         {
  16.                 this.name= name;
  17.                 this.en = en;
  18.                 this.end = end;
  19.                 this.end1 = end1;
  20.                 zongfen = en + end +end1;
  21.         }
  22.         public int compareTo(Student s)
  23.         {
  24.                 int num = new Integer(this.zongfen).compareTo(new Integer(s.zongfen));
  25.                 if(num==0)
  26.                         return this.name.compareTo(s.name);
  27.                 return num;
  28.                
  29.         }
  30.         public int hashCode()
  31.         {
  32.                 return 60;
  33.         }
  34.         public boolean equals(Object obj )
  35.         {
  36.                 if(!(obj instanceof Student))
  37.                         throw new RuntimeException("类型不匹配");
  38.                 Student s= (Student)obj;
  39.                 return this.name.equals(s.name) && this.zongfen==s.zongfen;
  40.         }
  41.         public String getName()
  42.         {
  43.                 return this.name;
  44.         }
  45.         public int getFen()
  46.         {
  47.                 return zongfen;
  48.         }
  49.        
  50. }
  51. class StudentTool
  52. {
  53.         public static Set<Student> show()throws IOException
  54.         {
  55.                 BufferedReader br =
  56.                                 new BufferedReader(new InputStreamReader(System.in));
  57.                 Set<Student> set= new TreeSet<Student>();
  58.                
  59.                 String len= null;
  60.                 while((len = br.readLine())!=null)
  61.                 {
  62.                         if("over".equals(len))
  63.                                 break;
  64.                         String[] arr = len.split(",");
  65.                         Student s= new Student(arr[0],Integer.parseInt(arr[1])
  66.                                                                                    ,Integer.parseInt(arr[2])
  67.                                                                                                 ,Integer.parseInt(arr[3]));
  68.                         set.add(s);
  69.                 }
  70.                 br.close();
  71.                         return set;
  72.         }
  73.         public static void run(Set<Student> set)throws IOException
  74.         {
  75.                 BufferedWriter bw = new BufferedWriter(new FileWriter("stu.txt"));
  76.                 for(Student se : set)
  77.                 {
  78.                         bw.write(se.getName());
  79.                         bw.write(se.getFen());
  80.                         bw.newLine();
  81.                         bw.flush();
  82.                
  83.                 }
  84.                 bw.close();
  85.        
  86.         }
  87. }
  88. class IOTestDemo
  89. {
  90.         public static void main(String[] args) throws IOException
  91.         {
  92.                 Set<Student> set= StudentTool.show();
  93.                 StudentTool.run(set);
  94.         }
  95. }
复制代码

1 个回复

倒序浏览
75行要写完整路径名:BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\stu.txt"));79行要类型转换 bw.write(Integer.toString(se.getFen()));
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马