本帖最后由 郝福明 于 2013-1-5 08:58 编辑
public static void print() {
String name = null;
int sum = 0;
int i=0;
String score=null;
boolean flag = true;
Scanner sc = null;
BufferedWriter bfw = null;
Set<Student> sets = null;
while(i!=1) {
Grade grade = new Grade();
sc = new Scanner(System.in);
System.out.println("请输入姓名");
name = sc.next();
System.out.println("请输入" + CourseType.chinese + "成绩");
grade.chineseGrade = sc.nextInt();
System.out.println("请输入" + CourseType.math + "成绩");
grade.mathGrade = sc.nextInt();
System.out.println("请输入" + CourseType.english + "成绩");
grade.englishGrade = sc.nextInt();
System.out.println("请入选择是否继续输入,输入1暂停,输入0继续");
i=sc.nextInt();
sets = new TreeSet(new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
int result = s1.getSum() - s2.getSum();
if (result == 0) {
result = s1.getName().compareTo(s2.getName());
}
return result;
}
});
sum = grade.chineseGrade + grade.mathGrade + grade.englishGrade;
sets.add(new Student(name, sum));
score="语文成绩"+grade.chineseGrade + "数学成绩" + grade.mathGrade + "英语成绩" + grade.englishGrade;
}
try {
bfw = new BufferedWriter(new FileWriter("D:\\NewFile\\exam\\src\\com\\itheima\\stu.txt"));
while(sets.size()!=0){
for(Student s:sets){
bfw.write(s.getName()+" "+score+"总成绩"+sum);
bfw.newLine();
bfw.flush();
}
}
} catch (IOException e) {
e.printStackTrace();
}finally{
if(bfw!=null){
try {
bfw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
当我输入1结束的时候开始往txt里写,但是就是停止不了。一直写下去了,怎样让它停止啊。还有一个问题就是为什么sets的第二次输入会覆盖掉第一个?
|