黑马程序员技术交流社区

标题: 毕老师视频教程21天-09练习中遇到的问题??? [打印本页]

作者: 孙国军    时间: 2012-4-24 11:11
标题: 毕老师视频教程21天-09练习中遇到的问题???
  1. /*
  2. 需求:
  3. 有五个学生,每个学生有3门课程的成绩,从键盘输入以上数据(包括姓名,三名课的成绩),输入的格式:
  4. 张三,30,40,60计算出总成绩,并把学生的信息和计算出的总分数低高顺序存放在磁盘文件"studentInfo.txt"中;
  5. 思路:
  6. 1.因为有五个学生对象,它们属于同一类事物,所以要建立一个类,用于描述他们;
  7. 2.因为学生的信息需要排序,对象的排序,我们自然的想到TreeSet集合;
  8. 3.最后,我们要定义一个IO输出流,将集合中的学生信息写入到指定的文件中去;
  9. */
  10. import java.util.*;
  11. import java.io.*;
  12. class StudentInfo
  13. {
  14. public static void main(String[] args)
  15. {
  16. StudentInfoTool.sortStudent();
  17. }
  18. }
  19. //学生工具类,用于建立学生对象,通过输入流获取学生信息,并把学生对象存入到Set集合中,在Set集合中自动排序后,使用输出流,将学生信息写入到指定的文件中去;
  20. class StudentInfoTool
  21. {
  22. public static void sortStudent()
  23. {
  24. Set<Student> studentInfo=new TreeSet<Student>();

  25. File file=new File("studentInfo.txt");

  26. BufferedWriter bufw=null;

  27. BufferedReader bufr=null;

  28. try
  29. {
  30. if (file.exists())
  31. {
  32. file.createNewFile();
  33. }
  34. bufw=new BufferedWriter(new FileWriter(file));

  35. bufr=new BufferedReader(new InputStreamReader(System.in));


  36. try
  37. {
  38. String line;
  39. while ((line=bufr.readLine())!=null)
  40. {
  41. if (line.equals("over"))
  42. {
  43. break;
  44. }
  45. String[] info=line.split(",");
  46. studentInfo.add(new Student(info[0],Double.parseDouble(info[1]),Double.parseDouble(info[2]),Double.parseDouble(info[3])) );
  47. }
  48. }
  49. catch (ClassCastException c)
  50. {
  51. throw new ClassCastException("类型不匹配!");
  52. }

  53. for (Student s:studentInfo )
  54. {
  55. bufw.write(s.toString());
  56. bufw.newLine();
  57. bufw.flush();
  58. }
  59. }
  60. catch (IOException io)
  61. {
  62. throw new RuntimeException("输出流,输入流读取失败!");
  63. }
  64. finally
  65. {
  66. try
  67. {
  68. if (bufw!=null)
  69. {
  70. bufw.close();
  71. }
  72. }
  73. catch (IOException i)
  74. {
  75. throw new RuntimeException("输出流关闭失败!");
  76. }
  77. try
  78. {
  79. if (bufr!=null)
  80. {
  81. bufr.close();
  82. }
  83. }
  84. catch (IOException i)
  85. {
  86. throw new RuntimeException("输入流关闭失败!");
  87. }
  88. }
  89. }
  90. }
  91. class Student implements Comparable<Student>
  92. {
  93. //声明表示学生属性的变量;
  94. private String name;
  95. private double chineseScore,mathScore,englishScore,sumScore;

  96. //Student类的构造函数;
  97. Student(String name,double chineseScore,double mathScore,double englishScore)
  98. {
  99. this.name=name;
  100. this.chineseScore=chineseScore;
  101. this.mathScore=mathScore;
  102. this.englishScore=englishScore;
  103. this.sumScore=chineseScore+mathScore+englishScore;
  104. }
  105. //定义学生属性的get和set方法;
  106. public void setName(String name)
  107. {
  108. this.name=name;
  109. }
  110. public String getName()
  111. {
  112. return name;
  113. }
  114. public void setChineseScore(double chineseScore)
  115. {
  116. this.chineseScore=chineseScore;
  117. }
  118. public double getMathScore()
  119. {
  120. return mathScore;
  121. }
  122. public void setEnglishScore(double englishScore)
  123. {
  124. this.englishScore=englishScore;
  125. }
  126. public double getEnglishScore()
  127. {
  128. return englishScore;
  129. }
  130. //复写继承至Comparable接口的compareTo方法;
  131. public int compareTo(Student s)
  132. {
  133. if (Double.valueOf(this.sumScore).compareTo(Double.valueOf(s.sumScore))==0)
  134. {
  135. return this.name.compareTo(s.name);
  136. }
  137. return Double.valueOf(this.sumScore).compareTo(Double.valueOf(s.sumScore));
  138. }
  139. //应为学生对象可能会存入到hashSet集合中,所以复写hashCode()和equals();
  140. public int hashCode()
  141. {
  142. return this.name.hashCode()+(int)this.sumScore*38;
  143. }
  144. public boolean equals(Student s)
  145. {
  146. return this.name.equals(s.name)&&(this.sumScore==s.sumScore);
  147. }
  148. //复写继承至Object类中的toString(),便于输出打印;
  149. public String toString()
  150. {
  151. return this.name+"\t"+this.chineseScore+"\t"+this.mathScore+"\t"+this.englishScore+"\t"+this.sumScore;
  152. }
  153. }
复制代码



问题在图片上
作者: 王杰    时间: 2012-4-24 12:32
153.  public boolean equals(Student s)
你这是将父类的equals Method重载了,并没有覆盖!
public boolean equals(Object obj){.....}
作者: 孙国军    时间: 2012-4-24 14:06
王杰 发表于 2012-4-24 12:32
153.  public boolean equals(Student s)
你这是将父类的equals Method重载了,并没有覆盖!
public boolea ...

先谢谢你了,equals()那我修改过了,

但是运行后还是,老样子
作者: 王杰    时间: 2012-4-24 16:13
孙国军 发表于 2012-4-24 14:06
先谢谢你了,equals()那我修改过了,

但是运行后还是,老样子

160.return this.name+"\t"+this.chineseScore+"\t"+this.mathScore+"\t"+this.englishScore+"\t"+this.sumScore;
\t:输出一个制表位,姓名和成绩之间会有一个制表位的距离。
作者: 孙国军    时间: 2012-4-24 16:54
王杰 发表于 2012-4-24 16:13
160.return this.name+"\t"+this.chineseScore+"\t"+this.mathScore+"\t"+this.englishScore+"\t"+this.s ...

这个我知道,但是为什么就单独的zhangshan后面会有能,

按道理应该都有啊





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