黑马程序员技术交流社区

标题: 刚学IO写的学生信息录入 [打印本页]

作者: 黑马TYR    时间: 2015-4-23 00:12
标题: 刚学IO写的学生信息录入
public class FileOutputStreamDemo3 {
public static void main(String[] args) throws IOException {
                TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>(){
                        public int compare(Student s1,Student s2){
                                return (getIn(s1.getChinese())+getIn(s1.getEnglish())
                                                +getIn(s1.getMath()))
                                                -(getIn(s2.getChinese())+getIn(s2.getEnglish())
                                                                +getIn(s2.getMath()));
                        }
                });
                Scanner sc = new Scanner(System.in);
                for (int i = 0; i < 2; i++) {
                        Student stu = new Student();
                        System.out.println("请输入学生姓名");
                        String name = sc.nextLine();
                        stu.setName(name);
                        System.out.println("请输入语文成绩");
                        String chinese = sc.nextLine();
                        stu.setChinese(chinese);
                        System.out.println("请输入英语成绩");
                        String english = sc.nextLine();
                        stu.setEnglish(english);
                        System.out.println("请输入数学成绩");
                        String math = sc.nextLine();
                        stu.setMath(math);
                        ts.add(stu);
                }
                BufferedWriter bw = new BufferedWriter(new FileWriter("f:\\s.txt"));
                bw.write("学生的信息如下");
                bw.flush();
                bw.write("姓名:语文成绩,英语成绩,数学成绩");
                bw.close();
                for(Student s:ts){
                        StringBuffer sb = new StringBuffer();
                        sb.append(s.getName()).append(",").append(s.getChinese())
                        .append(s.getEnglish()).append(",").append(s.getMath());
                        bw.write(sb.toString());
                        bw.newLine();
                        bw.flush();
                        bw.close();
                }
}
        public static int getIn(String st){
                return Integer.parseInt(st);
        }
}





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2