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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 王龙 中级黑马   /  2013-4-14 18:27  /  1253 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

/*
* 1、 有五个学生,每个学生有3门课(语文、数学、英语)的成绩,写一个程序接收从键盘输入学生的信息,
* 输入格式为:name,30,30,30(姓名,三门课成绩),然后把输入的学生信息按总分从高到低的顺序写入
* 到一个名称"stu.txt"文件中。要求:stu.txt文件的格式要比较直观,打开这个文件,就可以很清楚的看到学生的信息。
* 分析:
* 1.输入格式格式为name,30,30,30,一定用到构造函数
* 2.把输入的学生信息按总分从高到低的顺序写入,用到集合中的排序,而且有对应关系,所以用TreeMap
* 3.到一个名称"stu.txt"文件中,用到流的知识
*/
import java.io.*;
import java.util.*;


public class Test01
{
        public static void main(String[] args) {
                String str="c:/stu.txt";
                StudentCopy(str);
                       
                }
        public static void StudentCopy(String str){
                Map<Student,Double> map=new TreeMap<Student,Double>();
               
                for(int x=1;x<3;x++){
                        Scanner input=new Scanner(System.in);
                        System.out.println("请输入第个学生的姓名:");
                        String getName=input.next();       
                        System.out.println("请输入第个学生的语文成绩:");
                        double getChinese=input.nextDouble();
                        System.out.println("请输入第个学生的英语成绩:");
                        double getEnglish=input.nextDouble();
                        System.out.println("请输入第个学生的数学成绩:");
                        double getMath=input.nextDouble();
                        double getTotalScore=(getChinese+getEnglish+getMath)/3;
                        map.put(new Student(getName,getChinese,getEnglish,getMath), getTotalScore);
                       
                       
                       
               
                FileOutputStream fos=null;
                try {
                        fos=new FileOutputStream(str);
                        Set set=map.entrySet();
                        Iterator it=set.iterator();
                        while(it.hasNext()){
                                String dd=((Student) it.next()).toString();
                                fos.write(dd.getBytes());
                                System.out.println();
                        }
                        fos.flush();
                       
                } catch (Exception e) {
                        e.printStackTrace();
                }finally{
                        if(fos!=null){
                                try {
                                        fos.close();
                                } catch (Exception e) {
                                        e.printStackTrace();
                                }
                               
                        }
                }
               
        }
        }
}       
//Student类
class Student{
        private String name;
        private double ChineseScore;
        private double MathScore;
        private double EnglishScore;
        //private double TotalScore=ChineseScore+MathScore+EnglishScore;
        public Student(String name,double ChineseScore,double MathScore,double EnglishScore){
                this.name=name;
                this.ChineseScore=ChineseScore;
                this.MathScore=MathScore;
                this.EnglishScore=EnglishScore;
        }
       
}



你们复制到你们的机器上试试

评分

参与人数 1技术分 +1 收起 理由
冯海霞 + 1

查看全部评分

3 个回复

倒序浏览
分析不完全错.
1.不一定要用非要用构造函数,甚至下面的2条全都不需要就可以完成
2.跟treemap没关系
3.用啥流的知识?fileoutputstream写就行了...

评分

参与人数 1技术分 +1 收起 理由
冯海霞 + 1

查看全部评分

回复 使用道具 举报
本帖最后由 liuyangyang 于 2013-4-14 20:57 编辑

你好,
你可以参考下。        
public static void main(String[] args)throws Exception
        {
                BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                FileWriter fw=new FileWriter("f:\\1.txt");
                TreeSet<Student> ts=new TreeSet<Student>();
                for(int i=0;i<5;i++)
                {
                        String infor=br.readLine();
                        String[] str=infor.split(",");
                        ts.add(new Student(str[0],Double.parseDouble(str[1]),Double.parseDouble(str[2]),Double.parseDouble(str[3])));
                }
                for(Student t:ts)
                {
                        fw.write(t.toString()+"\r\t");
                }
                br.close();
                fw.close();
        }



学生类你应该会写吧,叫学生类实现comparable接口

评分

参与人数 1技术分 +1 收起 理由
冯海霞 + 1

查看全部评分

回复 使用道具 举报
Exception in thread "main" java.lang.ClassCastException: Student cannot be cast
to java.lang.Comparable
        at java.util.TreeMap.compare(TreeMap.java:1188)
        at java.util.TreeMap.put(TreeMap.java:531)
        at Test01.StudentCopy(Test01.java:26)
        at Test01.main(Test01.java:9)
你说的是这个问题吗,原因在于TreeMap的低层数据结构是二叉树,还有就是想在这个集合中存储元素必须让其元素具备比较性,要不就上这个集合具备比较性,我想你应该明白了吧,还有就是这个题目毕老师讲过的。

评分

参与人数 1技术分 +1 收起 理由
冯海霞 + 1

查看全部评分

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