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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

陈果

中级黑马

  • 黑马币:28

  • 帖子:17

  • 精华:0


  1. import java.io.BufferedReader;
  2. import java.io.BufferedWriter;
  3. import java.io.FileWriter;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.util.Collections;
  7. import java.util.Comparator;
  8. import java.util.Set;
  9. import java.util.TreeSet;

  10. /*
  11. * 把学生成绩存储在stu.txt文件中,并按照总分由高到低的顺序排序
  12. */

  13. public class StudentInfoTest {

  14.         public static void main(String[] args) {
  15.                 Comparator<Student> comp=Collections.reverseOrder();
  16.                 Set<Student> set=StuInfoTool.getInfo(comp);
  17.                 StuInfoTool.WriteInfoToTest(set);
  18.         }

  19. }

  20. //定义工具类
  21. class StuInfoTool{
  22.        
  23.         //通过键盘获取学生成绩
  24.         public static Set<Student> getInfo(){
  25.                 return getInfo(null);
  26.         }
  27.         public static Set<Student> getInfo(Comparator<Student> comp){                               
  28.                 BufferedReader br=null;
  29.                 Set<Student> set=null;
  30.                 if(comp==null){
  31.                         set=new TreeSet<Student>();
  32.                 }else{
  33.                         set=new TreeSet<Student>(comp);
  34.                 }
  35.                 try{
  36.                         br=new BufferedReader(new InputStreamReader(System.in));
  37.                         String line=null;
  38.                         while((line=br.readLine())!=null){
  39.                                 if(line.equals("over")){
  40.                                         break;
  41.                                 }
  42.                                 String[] info=line.split(",");
  43.                                 Student stu=new Student(info[0],Integer.parseInt(info[1]),Integer.parseInt(info[2]),Integer.parseInt(info[3]));
  44.                                 set.add(stu);                       
  45.                         }
  46.                 }catch(IOException e){
  47.                         throw new RuntimeException("输入失败");
  48.                 }finally{
  49.                         try {
  50.                                 if (br != null) {
  51.                                         br.close();
  52.                                 }
  53.                         } catch (IOException e2) {
  54.                                 throw new RuntimeException("输入流关闭失败");
  55.                         }
  56.                 }
  57.                 return set;
  58.         }
  59.        
  60.         //将获取的信息保存到文本文件中
  61.         public static void WriteInfoToTest(Set<Student> set){
  62.                 BufferedWriter bw=null;
  63.                 try{
  64.                         bw=new BufferedWriter(new FileWriter("stuinfo.txt"));
  65.                         for(Student stu:set){
  66.                                 bw.write(stu.toString());
  67.                                 bw.newLine();
  68.                                 bw.flush();
  69.                         }
  70.                 }catch(IOException e){
  71.                         throw new RuntimeException("写入文本文件失败");
  72.                 }finally{
  73.                         try {
  74.                                 if (bw != null) {
  75.                                         bw.close();
  76.                                 }
  77.                         } catch (IOException e2) {
  78.                                 throw new RuntimeException("写入流关闭失败");
  79.                         }
  80.                 }
  81.         }
  82. }

  83. //学生类
  84. class Student implements Comparable<Student>{
  85.         private String name;
  86.         private int ch,ma,en,sum;       
  87.        
  88.         public Student(String name, int ch, int ma, int en) {
  89.                 this.name = name;
  90.                 this.ch = ch;
  91.                 this.ma = ma;
  92.                 this.en = en;               
  93.                 sum=ch+ma+en;
  94.         }
  95.        
  96.         @Override
  97.         public int hashCode() {
  98.                 return name.hashCode()+sum;
  99.         }
  100.        
  101.         @Override
  102.         public boolean equals(Object obj) {
  103.                 if(!(obj instanceof Student)){
  104.                         throw new RuntimeException("类型不匹配");
  105.                 }
  106.                 Student stu=(Student)obj;
  107.                 return this.name.equals(stu.name) && this.ch==stu.ch && this.ma==stu.ma && this.en==stu.en;
  108.         }

  109.         @Override
  110.         public int compareTo(Student stu) {
  111.                 int num=new Integer(this.sum).compareTo(new Integer(stu.sum));
  112.                 if(num==0){
  113.                         return this.name.compareTo(stu.name);
  114.                 }
  115.                 return sum;
  116.         }
  117.        
  118.         @Override
  119.         public String toString() {
  120.                 return name+":"+ch+" "+ma+" "+en+"\t"+sum;
  121.         }
  122. }
复制代码


评分

参与人数 1技术分 +1 收起 理由
菜小徐 + 1

查看全部评分

6 个回复

正序浏览
treeset比较需要覆盖hashcode和equals方法?
回复 使用道具 举报
陈果 发表于 2014-3-19 22:57
写错了,我太粗心了,应该是num

呵……同道中人,我也经常干这种事,都有经验了
回复 使用道具 举报
方青木 发表于 2014-3-19 22:53
public int compareTo(Student stu) {

119.                int num=new Integer(this.sum).compareTo(new ...

写错了,我太粗心了,应该是num
回复 使用道具 举报

嗯,就是这个地方,改了以后就对了
回复 使用道具 举报
public int compareTo(Student stu) {

119.                int num=new Integer(this.sum).compareTo(new Integer(stu.sum));

120.                if(num==0){

121.                        return this.name.compareTo(stu.name);

122.                }

123.                return sum;

124.        }
返回sum是啥意思啊

评分

参与人数 1技术分 +1 收起 理由
滔哥 + 1

查看全部评分

回复 使用道具 举报
123行  return sum;错啦
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马