本帖最后由 周刚 于 2012-7-9 20:31 编辑
出现这个错误是因为逻辑有点乱,在执行添加到集合之前,并没有对sum赋值 ,而仅仅是在写入到文件时才赋值。所以在集合中没有按规定排序,输入到
文件中自然也就没有按要求排序了。
public int compareTo(Student o) {
//System.out.println(this.getSum());//通过这一句打印可以看出:
if (this.getSum() > o.getSum()) { //在存到硬盘之前并没有调用setSumm赋值,而排序是在存盘前
//添加对象到Set里面时就开始排序了,但此时sum并没有赋值,都是0。
return 1;
}
作如下修改:
public double getSum() {
return chineseGrade+mathGrade+enGrade;
}
完整代码:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.TreeSet;
class Student implements Comparable<Student> {
private String name; // 姓名
private double chineseGrade; // 语文
private double mathGrade; // 数学
private double enGrade; // 英语
private double sum; // 总成绩
public Student(String name, double chineseGrade, double mathGrade,
double enGrade) {
this.name = name;
this.chineseGrade = chineseGrade;
this.mathGrade = mathGrade;
this.enGrade = enGrade;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getChineseGrade() {
return chineseGrade;
}
public void setChineseGrade(double chineseGrade) {
this.chineseGrade = chineseGrade;
}
public double getMathGrade() {
return mathGrade;
}
public void setMathGrade(double mathGrade) {
this.mathGrade = mathGrade;
}
public double getEnGrade() {
return enGrade;
}
public void setEnGrade(double enGrade) {
this.enGrade = enGrade;
}
public double getSum() {
return chineseGrade+mathGrade+enGrade; }
public void setSum(double sum) {
this.sum = sum;
}
@Override
public int compareTo(Student o) {
//System.out.println(this.getSum());//通过这一句打印可以看出:
if (this.getSum() > o.getSum()) { //在招待存硬之前并没有调用setSumm赋值,而排序是在存盘前
//添加对象到Set里面时就开始序了。 return 1;
}
if (this.getSum() == o.getSum()) {
return this.name.compareTo(o.name);
}
return -1;
}
}
// 信息处理类,输入学生信息并保存到文件E:\\stu.txt上
class InfoDeal {
private TreeSet<Student> set;
InfoDeal() {
set = new TreeSet<Student>();
}
public void start() {
// 从键盘上输入学生的信息
inputInfo();
// 将学生信息存放在磁盘文件中
saveInfoToFile();
}
// 从键盘上输入学生的信息
public void inputInfo() {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = null;
String info[] = null;
try {
// 输入的格式为 "学生,语文,数学,英语"
while ((s = br.readLine()) != null) {
if (s.equals("886")) {
break;
}
info = s.split(",");
setStudentInfo(info[0], Double.parseDouble(info[1]),
Double.parseDouble(info[2]),
Double.parseDouble(info[1]));
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
// 将学生信息存放在磁盘文件"E:\\stu.txt"
public void saveInfoToFile() {
BufferedWriter bw = null;
Iterator<Student> it;
Student stud = null;
try {
bw = new BufferedWriter(new FileWriter("E:\\Stud.txt"));
it = set.iterator();
while (it.hasNext()) {
stud = it.next();
// 把信息保存到文件中
Double dd = stud.getChineseGrade();
Double ss = stud.getEnGrade();
Double cc = stud.getMathGrade();
stud.setSum(dd + ss + cc); //这里才对sum赋值,显然太晚了!!!
bw.write(stud.getName() + "——" + "总成绩:" + stud.getSum());
bw.newLine();
bw.flush();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// 关闭输入流
if (bw != null) {
try {
bw.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
public void setStudentInfo(String name, double chineseGrade,
double mathGrade, double enGrade) {
set.add(new Student(name, chineseGrade, mathGrade, enGrade));
}
}
public class Test1 {
public static void main(String[] args) {
new InfoDeal().start();
}
}
录入数据如下:
a,10,10,10
b,20,20,20
c,5,5,5
886
输出结果如下:
c——总成绩:15.0
a——总成绩:30.0
b——总成绩:60.0 |