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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

/*
练习:将学生姓名和语数外三门课的分数录入,然后将三门分数加和,按照总分由高到低及
          姓名默认排序的顺序存储到studentinfo.txt文件中。

思路:1.描述学生类
          2.通过键盘获取学生的数据,并将该行中的数据取出封装为学生对象
          3.定义一个可操作学生对象的工具类
          4.要将多个学生的信息存储起来且要排序,所以选用TreeSet集合
          5.将集合中的信息写入到文件中
*/

package io;

import java.util.*;
import java.io.*;

//新建一个学生类
class Student implements Comparable<Student>                //使得学生类具备比较性
{
        private String name;
        private int CN;
        private int MA;
        private int EN;
        private int sum;
        //构造函数要求内容为String name, int CN, int MA, int EN
        Student(String name, int CN, int MA, int EN)
        {
                this.name = name;
                this.CN = CN;
                this.MA = MA;
                this.EN = EN;
                this.sum = CN+MA+EN;
        }
        //继承Comparable接口必须覆盖compareTo方法,其内部比较用compareTo方法,且类内部要覆盖hashCode和equals两个方法,
        //为默认比较提供功能,注意此处整数的比较要建立基本数据类型对象包装类,equals方法中的比较与此不同,详见下
        public int compareTo(Student s)
        {
                int num = new Integer(this.sum).compareTo(new Integer(s.sum));
                if (num==0)
                {
                        return this.name.compareTo(s.name);
                }
                return num;
        }
        public String getName()
        {
                return name;
        }
        public int getSum()
        {
                return sum;
        }
        public int hashCode()
        {
                return name.hashCode()+sum*53;
        }
        public boolean equals(Object obj)
        {
                if(!(obj instanceof Student))
                        throw new ClassCastException("类型不一致");
                Student s = (Student)obj;
                //equals中字符串的比较用equals方法,数字的比较用==
                return this.name.equals(s.name) && this.sum==s.sum;
        }
        public String toString()
        {
                return name+":语文="+CN+";数学="+MA+";英语="+EN;
        }
}

class StudentInfoTool
{
        //注意:集合上要加泛型
        public static TreeSet<Student> getStudents() throws IOException
        {
                return getStudents(null);
        }
        public static TreeSet<Student> getStudents(Comparator<Student> cmp) throws IOException
        {
                BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));                //建立一个读取流缓冲区
                String line = null;
                TreeSet<Student> stus = null;
                if (cmp==null)
                        stus = new TreeSet<Student>();                //建立一个TreeSet集合
                else
                        stus = new TreeSet<Student>(cmp);
                while ((line=bufr.readLine())!=null)
                {
                        if ("over".equals(line))
                                break;
                        String[] info = line.split(",");        //建立一个字符串数组用来存放分割后的数据元素       
                        Student stu = new Student(info[0], Integer.parseInt(info[1]),
                                                                                                Integer.parseInt(info[2]),
                                                                                                Integer.parseInt(info[3]));                //建立一个学生类对象存储之前数组红的内容并
                                                                                                                                                                //进行比较
                        stus.add(stu);
                }
                bufr.close();
                return stus;
        }

        public static void writeToFile(TreeSet<Student> stus) throws IOException
        {
                BufferedWriter bufw = new BufferedWriter(new FileWriter("studentinfo.txt"));
                for (Student s : stus)
                {
                        bufw.write(s.toString()+"\t");
                        bufw.write(s.getSum()+"");                //此处,sum即总分原为整数变为字符串后可避免乱码的产生
                        bufw.newLine();
                        bufw.flush();
                }
                bufw.close();
        }
}

class StudentInfo
{
        public static void main(String[] args) throws IOException
        {
                Comparator<Student> cmp = Collections.reverseOrder();                //通过Collections类中的reverseOrder方法构造一个反序的比较器
                TreeSet<Student> stus = StudentInfoTool.getStudents(cmp);                //新建一个Student的TreeSet集合并调用StudentInfoTool中
                                                                                                                                                //的getStudents方法
                StudentInfoTool.writeToFile(stus);                //调用writeToFile方法将数据输入到指定文件
        }
}

54 个回复

正序浏览
姚志昊 发表于 2015-9-4 22:15
我今天也学到这里了。。

加油!!
回复 使用道具 举报
我今天也学到这里了。。
回复 使用道具 举报 1 0
foreverfun 发表于 2015-9-4 20:07
代码只能看懂一部分,且要自己敲出来就更加难了

没事,多加练习,多敲代码,敲着敲着就熟了
回复 使用道具 举报
王乙帆 发表于 2015-9-4 20:13
多分享几天的哈哈~~

好的,没问题
回复 使用道具 举报
nightwish 发表于 2015-9-4 20:20
我还没看到这里,表示要抓紧了

不着急,慢慢来
回复 使用道具 举报
回复 使用道具 举报
kitboxer 发表于 2015-9-3 22:32
我去,果然很厉害。

学过去也就那么回事,没什么的
回复 使用道具 举报
kitboxer 发表于 2015-9-3 22:32
我去,果然很厉害。

学过去也就那么回事,没什么的
回复 使用道具 举报
双元黑马12 发表于 2015-9-3 21:40
面向对象 是不是学后面的 基础啊

对,我现在也有点忘了
回复 使用道具 举报
支持支持
回复 使用道具 举报
15210373205 发表于 2015-9-3 21:27
没有学到,有点印象

加油~~~~~
回复 使用道具 举报
panpanai 发表于 2015-9-3 20:48
感觉只看懂了一半,面向对象都快学完了。

不着急,慢慢来
回复 使用道具 举报
我还没看到这里,表示要抓紧了
回复 使用道具 举报
多分享几天的哈哈~~
回复 使用道具 举报
代码只能看懂一部分,且要自己敲出来就更加难了
回复 使用道具 举报

基础打好了,不一定知识掌握的多好,把思想学好了
回复 使用道具 举报
好多啊  看不懂!!!!!!!!!!!!!!
回复 使用道具 举报

基础班的两倍差不多吧
回复 使用道具 举报
我去,果然很厉害。
回复 使用道具 举报
123下一页
您需要登录后才可以回帖 登录 | 加入黑马