A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 黑马TYR 中级黑马   /  2015-4-23 00:12  /  335 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

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);
        }
}

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马