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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

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

class StudText
{
        public static void main(String[] args)throws IOException
        {
                Set<StudentCard> scc = StuTool.getStudent();
                StuTool.creFile(scc);
        }
}


//创建的学生对象
class Student implements Comparable<Student>
{
        private String name;
        private int math , yuwen , english; //三科成绩
        private int sum = 0;
        StudentCard(String name , int math , int lang , int forlang)
        {
                this.name = name;
                this.math = math;
                this.lang = lang;
                this.forlang = forlang;
                sum = math + lang + forlang; //总分
        }
        public int hashCode()
        {
                return name.hashCode() + sum*20;
        }
        public boolean equals(Object obj)
        {
                if(!(obj instanceof StudentCard))
                        throw new RuntimeException("类型不匹配");
                StudentCard sc = (StudentCard)obj;
                return this.name.equals(name) && this.sum == sum;
        }
        public int compareTo(StudentCard sc)
        {
                int num = new Integer(this.sum).compareTo(new Integer(sum));
                        if(num == 0)
                                return this.name.compareTo(name);
                return num;
        }
        public int getSum()
        {
                return sum;
        }
        public String toString()
        {
                return "姓名:" +name + "        成绩:" +math + "," +yuwsen +"," + english;
        }
}

class StuTool
{
        public static Set<Student> getStudent() throws IOException
        {
                BufferedReader bur = new BufferedReader(new InputStreamReader(System.in));
                Set<Student>  stud = new TreeSet<Student>();
                String s = null;
                while((s = bur.readLine()) != null)
                {
                        if("over".equals(s))
                                break;
                        String[] arr = s.split(",");
                        Student  stu = new StudentCard(arr[0] , Integer.parseInt(arr[1]) ,
                                                                                            Integer.parseInt(arr[2]) ,
                                                                                            Integer.parseInt(arr[3]));
                        stud.add(stu);
                }
                bur.close();
                return stud;
        }

        public static void creFile(Set<Student> stud) throws IOException
        {
                BufferedWriter buw = new BufferedWriter(new FileWriter("stud.txt"));
                for(Student sc : stud)
                {
                        buw.write(sc.toString() + "\t");
                        buw.write(sc.getSum() + "");
                        buw.newLine();
                        buw.flush();
                }
                buw.close();
        }
}

跟视频对了好几遍,为什么生成的文件只有一行数据? 哪里有问题 郁闷


评分

参与人数 1技术分 +1 收起 理由
周志龙 + 1

查看全部评分

4 个回复

正序浏览
To 金牌黑马 2013-10-14 09:49:12
报纸
楼主你好,如果问题已解决请将帖子状态修改为提问结束,如果未解决请继续提问,谢谢合作
如果不会修改请看解释帖:http://bbs.itheima.com/thread-89313-1-1.html
回复 使用道具 举报
wodenhaowzg 发表于 2013-10-13 17:50
。。。刚才发帖改把StudentCard名字改成Student了,所以可能其中有地方没改过来
new FileWriter("stud.txt" ...

怎么会没有这个构造函数呢?你用的JDK 1.0吗,本来有2张截图给你的第一张是表示有个这个构造函数,第二张表示自从jdk1.1开始有这个类了。可惜不知道怎么上传图片。
回复 使用道具 举报
。。。刚才发帖改把StudentCard名字改成Student了,所以可能其中有地方没改过来
new FileWriter("stud.txt",true) FileWriter的构造函数没这个。。
我在eclipse运行 没语法错误 但键盘输入了好多行 文件里就存了第一行
回复 使用道具 举报
这是你手敲上去的代码吗? 我复制到我的Myeclipse里老多错误,不过话归正题, BufferedWriter buw = new BufferedWriter(new FileWriter("stud.txt"));改成BufferedWriter buw = new BufferedWriter(new FileWriter("stud.txt",true));多加一个true参数,不加true新写入的数据覆盖原来的来,加true接着原来的数据后面继续写

评分

参与人数 1技术分 +1 收起 理由
周志龙 + 1

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马