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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

本帖最后由 王明(1988) 于 2012-5-27 15:11 编辑

  1. package com.heima.test;

  2. import java.io.BufferedReader;
  3. import java.io.BufferedWriter;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileOutputStream;
  6. import java.io.FileWriter;
  7. import java.io.IOException;
  8. import java.io.InputStreamReader;
  9. import java.io.OutputStreamWriter;
  10. import java.util.Scanner;
  11. import java.util.Set;
  12. import java.util.TreeSet;

  13. /**
  14. * 6. 有五个学生,每个学生有3门课的成绩,从键盘输入以上数据(包括姓名,三门课成绩),
  15. * 计算出总成绩,并把学生的信息和计算出的总分数存放在磁盘文件"stud.txt"中。
  16. *
  17. * @author wangming1988
  18. *
  19. */
  20. public class StudentTreeSet {

  21. public static void main(String[] args) {
  22. Set<Student2> stuSet = getStudentSetByReadSystemIn();
  23. write2StuTxt(stuSet);
  24. }

  25. private static Set<Student2> getStudentSetByReadSystemIn() {
  26. System.out.println("姓名 语文 数学 英语");
  27. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  28. Set<Student2> stus = new TreeSet<Student2>();
  29. try {
  30. for (String line = null; (line = br.readLine()) != null;) {
  31. if ("exit".equals(line))
  32. break;
  33. Scanner scanner = new Scanner(line);
  34. String name = scanner.next();
  35. int chineseScore = scanner.nextInt();
  36. int mathScore = scanner.nextInt();
  37. int englishScore = scanner.nextInt();
  38. stus.add(new Student2(name, chineseScore, mathScore,
  39. englishScore));
  40. }
  41. } catch (IOException e) {
  42. e.printStackTrace();
  43. } finally {
  44. try {
  45. if (br != null)
  46. br.close();
  47. } catch (IOException e) {
  48. e.printStackTrace();
  49. }
  50. }
  51. return stus;
  52. }

  53. private static void write2StuTxt(Set<Student2> stus) {
  54. BufferedWriter bw = null;
  55. /*
  56. * bw = new BufferedWriter(new OutputStreamWriter( new
  57. * FileOutputStream("stuScore.txt")));
  58. */
  59. try {
  60. bw = new BufferedWriter(new FileWriter("stuScore.txt"));
  61. } catch (IOException e) {
  62. e.printStackTrace();
  63. }

  64. try {
  65. bw.write("姓名 语文 数学 英语 总分(升序)");
  66. bw.newLine();
  67. bw.flush();
  68. for (Student2 student2 : stus) {
  69. bw.write(student2.toString());
  70. bw.newLine();
  71. bw.flush();
  72. }
  73. } catch (IOException e) {
  74. e.printStackTrace();
  75. } finally {
  76. try {
  77. if (bw != null)
  78. bw.close();
  79. } catch (IOException e) {
  80. e.printStackTrace();
  81. }
  82. }
  83. }

  84. }

  85. /**
  86. * 被比较的对象也是Student2
  87. *
  88. * @author wangming1988
  89. *
  90. */
  91. class Student2 implements Comparable<Student2> {
  92. /* 姓名做主键吧 */
  93. private String name;

  94. private int chineseScore;

  95. private int mathScore;

  96. private int englishScore;

  97. private int total;

  98. public Student2(String name, int chineseScore, int mathScore,
  99. int englishScore) {
  100. super();
  101. this.name = name;
  102. this.chineseScore = chineseScore;
  103. this.mathScore = mathScore;
  104. this.englishScore = englishScore;
  105. }

  106. public String getName() {
  107. return name;
  108. }

  109. public void setName(String name) {
  110. this.name = name;
  111. }

  112. public int getChineseScore() {
  113. return chineseScore;
  114. }

  115. public void setChineseScore(int chineseScore) {
  116. this.chineseScore = chineseScore;
  117. }

  118. public int getMathScore() {
  119. return mathScore;
  120. }

  121. public void setMathScore(int mathScore) {
  122. this.mathScore = mathScore;
  123. }

  124. public int getEnglishScore() {
  125. return englishScore;
  126. }

  127. public void setEnglishScore(int englishScore) {
  128. this.englishScore = englishScore;
  129. }

  130. public int getTotal() {
  131. return this.mathScore + this.chineseScore + this.englishScore;
  132. }

  133. @Override
  134. public int hashCode() {
  135. final int prime = 31;
  136. int result = 1;
  137. result = prime * result + chineseScore;
  138. result = prime * result + englishScore;
  139. result = prime * result + mathScore;
  140. result = prime * result + ((name == null) ? 0 : name.hashCode());
  141. result = prime * result + getTotal();
  142. return result;
  143. }

  144. @Override
  145. public boolean equals(Object obj) {
  146. if (this == obj)
  147. return true;
  148. if (obj == null)
  149. return false;
  150. if (getClass() != obj.getClass())
  151. return false;
  152. Student2 other = (Student2) obj;
  153. if (chineseScore != other.chineseScore)
  154. return false;
  155. if (englishScore != other.englishScore)
  156. return false;
  157. if (mathScore != other.mathScore)
  158. return false;
  159. if (name == null) {
  160. if (other.name != null)
  161. return false;
  162. } else if (!name.equals(other.name))
  163. return false;
  164. if (getTotal() != other.getTotal())
  165. return false;
  166. return true;
  167. }

  168. @Override
  169. public String toString() {
  170. return name + " " + chineseScore + " " + mathScore + " "
  171. + englishScore + " " + getTotal();
  172. }

  173. /**
  174. * 返回值为0,说明两人总分相同。 返回值小于0,说明小于你选取的比较对象。 返回值大于0,说明大于你选取的比较对象。
  175. */
  176. @Override
  177. public int compareTo(Student2 o) {
  178. int result = 0;
  179. if (o instanceof Student2) {
  180. Student2 stu = (Student2) o;
  181. result = stu.getTotal() - o.getTotal();
  182. }
  183. return result;
  184. }
  185. }
复制代码
上图:

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马