本帖最后由 张迁 于 2013-5-19 23:35 编辑
跟老师的代码对了好几遍,哪里出错了?
比如: 输入:张三,34,45,56
输出的是:张三,34,45,0 为什么第三个值穿不进去啊?- public class Exercise4 {
- public static void main(String[] args) throws IOException {
-
- Set<Student> set = new TreeSet<>();
- Scanner sc = new Scanner (System.in);
- System.out.println("请输入学生成绩:");
-
- while (sc.hasNextLine()) {
- String str = sc.nextLine();
- if ("quit".equals(str)) {
- sc = new Scanner( new FileInputStream("student.txt"));
- continue;
- }
- String[] arr = str.split(",");
- Student s = new Student(arr[0], Integer.parseInt(arr[1]), Integer.parseInt(arr[2]), Integer.parseInt(arr[3]));
- set.add(s);
-
- }
-
- BufferedWriter bw = new BufferedWriter(new FileWriter("student.txt"));
- for (Student student : set) {
- System.out.println(student);
- bw.write(student.toString());
- bw.newLine();
- }
- bw.close();
- }
复制代码 |