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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© ゞSunペ果果つ 中级黑马   /  2013-4-29 12:32  /  1765 人查看  /  8 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 ゞSunペ果果つ 于 2013-5-3 08:58 编辑


  1. <div class="blockcode"><blockquote>import java.io.*;
  2. import java.util.*;
  3. import java.lang.*;
  4. public class Test31
  5. {        
  6.         
  7.         public static void main(String[] args) throws IOException
  8.         {        
  9.                 Set<Student> students=StudentTool.getStudent();
  10.                 StudentTool.write2file(students);
  11.         }


  12. }
  13. class Student implements Comparable<Student>
  14. {
  15.         String name;
  16.         int ma,cn,en;
  17.         int sum;
  18.         public Student(String name,int ma,int cn,int en){
  19.                 this.name=name;
  20.                 this.ma=ma;
  21.                 this.cn=cn;
  22.                 this.en=en;
  23.                 this.sum=ma+cn+en;
  24.         }
  25.         public String getName(){
  26.                 return name;
  27.         }
  28.         public  int getSum(){
  29.                 return sum;
  30.         }
  31.         public int hashCode(){
  32.                 return name.hashCode()+sum*78;
  33.         }
  34.         public boolean equals(Object obj){
  35.                 if(!(obj instanceof Student))
  36.                         throw new ClassCastException("你输入的类有问题");
  37.                 Student s=(Student)obj;
  38.                 return this.name.equals(s.name)&& this.sum==s.sum;
  39.         }
  40.         public int compareTo(Student s){
  41.                 <font color="red">int n=new Integer(this.name).compareTo(new Integer(s.sum));</font>
  42.                 if(n==0)
  43.                         return this.name.compareTo(s.name);
  44.                 else
  45.                         return n;
  46.         }
  47.         public String toString(){
  48.                 return "student["+name+"-"+ma+"-"+cn+"-"+en+"]";
  49.         }
  50. }
  51. class StudentTool
  52. {
  53.         public static Set<Student> getStudent()throws IOException{
  54.                 BufferedReader buff=new BufferedReader(new InputStreamReader(System.in));
  55.                 String line=null;
  56.                  Set        <Student> students=new TreeSet<Student>();
  57.                 while((line=buff.readLine())!=null){
  58.                         //BufferedReader buff=new BufferedReader(new InputStreamReader(System.in));
  59.                         if(line.equals("ober")){
  60.                                 break;
  61.                         }
  62.                         String []stu=line.split(",");
  63.                         //System.out.println(stu[0],Integer.parseInt(stu[1]),Integer.parseInt(stu[2]));
  64.                         Student s=new Student(stu[0],Integer.parseInt(stu[1]),Integer.parseInt(stu[2]),Integer.parseInt(stu[3]));
  65.                         students.add(s);
  66.                 }
  67.                 buff.close();
  68.                 return students;

  69.         }
  70.         public static void write2file(Set<Student> set)throws IOException{
  71.                 File f=new File("a.txt");
  72.                 f.createNewFile();
  73.                 BufferedWriter buf=new BufferedWriter(new FileWriter(f));
  74.                 for(Student s:set){
  75.                         buf.write(s.toString());
  76.                         buf.write(s.getSum());
  77.                         buf.newLine();
  78.                         buf.flush();
  79.                 }
  80.                 buf.close();
  81.                
  82.         }
  83. }
复制代码
我没有写注释是希望大家运行一下就可以看到效果了,贴就贴出来呗,求助无望,最终决定重新来过...
昨天下午拿出了这份打印出来有错误的代码,我看了一眼,自己找到了错误。
        public int compareTo(Student s){
                int n=new Integer(this.name).compareTo(new Integer(s.sum));//就是这句的问题
                if(n==0)
                        return this.name.compareTo(s.name);
                else
                        return n;
        }

2013-04-30_085243.gif (6.38 KB, 下载次数: 0)

2013-04-30_085243.gif

点评

建议楼主在适当的地方加上相应的注释,看着会清晰些,谢谢  发表于 2013-4-29 23:43

评分

参与人数 1技术分 +1 收起 理由
黄玉昆 + 1

查看全部评分

8 个回复

倒序浏览
      刚粘贴好 还没看  首先我就想说 没有注释 看着好费劲啊
回复 使用道具 举报
花开花落总相似 发表于 2013-4-29 13:02
刚粘贴好 还没看  首先我就想说 没有注释 看着好费劲啊

:(我也不想啊,本来是自己写的,不是出现意外我也就不贴出来了,看看呗,有个问题
回复 使用道具 举报
报的啥异常啊,,把 异常 信息扔上来看看呗。。。
回复 使用道具 举报
董维明 发表于 2013-4-29 20:14
报的啥异常啊,,把 异常 信息扔上来看看呗。。。

:(你复制一下,运行一下不就看到了么?
回复 使用道具 举报
你这是类型错误,NumberFormatException是你传入的值的类型不对,后面出现input String s,表示你传入一个string类型给int类型,当然会出现类型格式错误,将string进行类型转换,如Integer.parseInt(字符串)就可以了。

评分

参与人数 1技术分 +1 收起 理由
黄玉昆 + 1

查看全部评分

回复 使用道具 举报
无解啊...........
回复 使用道具 举报
符立波 发表于 2013-4-30 10:14
无解啊...........

无解是啥意思啊?
回复 使用道具 举报
刘沛霞 发表于 2013-4-30 09:31
你这是类型错误,NumberFormatException是你传入的值的类型不对,后面出现input String s,表示你传入一个s ...

:)我本来就是那样做的,可以还是编译不了,你看看程序,
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马