A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区
传智教育官网黑马程序员官网
只需一步,快速开始
王勃
中级黑马
黑马币:-8
帖子:148
精华:0
© 王勃 中级黑马 / 2012-5-27 14:40 / 1585 人查看 / 0 人回复 / 0 人收藏 转载请遵从CC协议 禁止商业使用本文
package com.heima.test; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.util.Scanner; import java.util.Set; import java.util.TreeSet; /** * 6. 有五个学生,每个学生有3门课的成绩,从键盘输入以上数据(包括姓名,三门课成绩), * 计算出总成绩,并把学生的信息和计算出的总分数存放在磁盘文件"stud.txt"中。 * * @author wangming1988 * */ public class StudentTreeSet { public static void main(String[] args) { Set<Student2> stuSet = getStudentSetByReadSystemIn(); write2StuTxt(stuSet); } private static Set<Student2> getStudentSetByReadSystemIn() { System.out.println("姓名 语文 数学 英语"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); Set<Student2> stus = new TreeSet<Student2>(); try { for (String line = null; (line = br.readLine()) != null;) { if ("exit".equals(line)) break; Scanner scanner = new Scanner(line); String name = scanner.next(); int chineseScore = scanner.nextInt(); int mathScore = scanner.nextInt(); int englishScore = scanner.nextInt(); stus.add(new Student2(name, chineseScore, mathScore, englishScore)); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (br != null) br.close(); } catch (IOException e) { e.printStackTrace(); } } return stus; } private static void write2StuTxt(Set<Student2> stus) { BufferedWriter bw = null; /* * bw = new BufferedWriter(new OutputStreamWriter( new * FileOutputStream("stuScore.txt"))); */ try { bw = new BufferedWriter(new FileWriter("stuScore.txt")); } catch (IOException e) { e.printStackTrace(); } try { bw.write("姓名 语文 数学 英语 总分(升序)"); bw.newLine(); bw.flush(); for (Student2 student2 : stus) { bw.write(student2.toString()); bw.newLine(); bw.flush(); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (bw != null) bw.close(); } catch (IOException e) { e.printStackTrace(); } } } } /** * 被比较的对象也是Student2 * * @author wangming1988 * */ class Student2 implements Comparable<Student2> { /* 姓名做主键吧 */ private String name; private int chineseScore; private int mathScore; private int englishScore; private int total; public Student2(String name, int chineseScore, int mathScore, int englishScore) { super(); this.name = name; this.chineseScore = chineseScore; this.mathScore = mathScore; this.englishScore = englishScore; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getChineseScore() { return chineseScore; } public void setChineseScore(int chineseScore) { this.chineseScore = chineseScore; } public int getMathScore() { return mathScore; } public void setMathScore(int mathScore) { this.mathScore = mathScore; } public int getEnglishScore() { return englishScore; } public void setEnglishScore(int englishScore) { this.englishScore = englishScore; } public int getTotal() { return this.mathScore + this.chineseScore + this.englishScore; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + chineseScore; result = prime * result + englishScore; result = prime * result + mathScore; result = prime * result + ((name == null) ? 0 : name.hashCode()); result = prime * result + getTotal(); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Student2 other = (Student2) obj; if (chineseScore != other.chineseScore) return false; if (englishScore != other.englishScore) return false; if (mathScore != other.mathScore) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; if (getTotal() != other.getTotal()) return false; return true; } @Override public String toString() { return name + " " + chineseScore + " " + mathScore + " " + englishScore + " " + getTotal(); } /** * 返回值为0,说明两人总分相同。 返回值小于0,说明小于你选取的比较对象。 返回值大于0,说明大于你选取的比较对象。 */ @Override public int compareTo(Student2 o) { int result = 0; if (o instanceof Student2) { Student2 stu = (Student2) o; result = stu.getTotal() - o.getTotal(); } return result; } } 复制代码
100.png (862.98 KB, 下载次数: 13)
下载附件
2012-5-27 14:42 上传