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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© qiaojinhui 中级黑马   /  2013-5-11 10:14  /  1870 人查看  /  9 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 qiaojinhui 于 2013-5-12 13:59 编辑

定义一个学生类,需要姓名,年龄,考试成绩三个成员属性,创建对这五个对象按成绩排序,并将结果输出。用TreeSet和Comparator实现。  麻烦尽量注解详细一点。还有怎么才能有思路啊!为什么做题老是没思路,不知道怎么敲代码!

评分

参与人数 1技术分 +1 收起 理由
殇_心。 + 1 加油吧。哥们

查看全部评分

9 个回复

倒序浏览
import java.util.*;


public class Student implements Comparable{
        private String name ;
        private int age;
        private int score;
       
        public String getName() {
                return name;
        }
        public void setName(String name) {
                this.name = name;
        }
        public int getAge() {
                return age;
        }
        public void setAge(int age) {
                this.age = age;
        }
        public int getScore() {
                return score;
        }
        public void setScore(int score) {
                this.score = score;
        }
        public Student(String name ,int age,int score){
                this.name=name;
                this.age=age;
                this.score=score;
        };
       
        @Override
        public String toString() {
                return this.name;
        }
        public static void main(String[] args) {
                Student s1=new Student("刘一",20,100);
                Student s2=new Student("胡二",30,90);
                Student s3=new Student("张三",40,60);
                Student s4=new Student("李四",50,80);
                Student s5=new Student("王五",60,70);
                List <Student> list=new ArrayList();
                list.add(s1);
                list.add(s2);
                list.add(s3);
                list.add(s4);
                list.add(s5);
                Collections.sort(list);
                for(Student s : list){
                        System.out.println(s);       
                }
               
        }
        public int compareTo(Object o) {
                Student s=(Student)o;
                    return this.score-s.getScore();               
        }

}

评分

参与人数 1技术分 +1 收起 理由
殇_心。 + 1 以后写代码的时候 注意写个注释。.

查看全部评分

回复 使用道具 举报
能不能详细点啊
回复 使用道具 举报

import java.util.*;

public class Test10
{
        public static void main(String[] args)
        {
                //创建一个TreeSet集合
                TreeSet<Student> ts = new TreeSet<Student>(new Mycompare());
               
                //向集合添加五个学生对象
                ts.add(new Student("张三",19,88));
                ts.add(new Student("李四",18,97));
                ts.add(new Student("王五",21,85));
                ts.add(new Student("赵六",19,85));
                ts.add(new Student("钱七",22,93));
               
                //定义一个变量表示成绩排名
                int rank = 1;
               
                //循环遍历打印结果
                for (Student s : ts)
                        System.out.println((rank++)+"--"+s.getName()+"  "+s.getAge()+"  "+s.getScore());
        }
}

class Student
{
        //定义成员属性
        private String name;
        private int age;
        private double score;
       
        //创建有参数构造函数,要求成员创建就要对属性初始化
        Student(String name,int age,double score)
        {
                this.name = name;
                this.age = age;
                this.score = score;
        }
       
        //创建set方法便于修改成员属性
                public void setName()
                {
                        this.name =  name;
                }
                public void setAge()
                {
                        this.age = age;
                }
                public void setScore()
                {
                        this.score = score;
                }
        //创建获取属性的get方法
        public String getName()
        {
                return name;
        }
        public int getAge()
        {
                return age;
        }
        public double getScore()
        {
                return score;
        }
}

//自定义比较器,主要是按照成绩由高到低排序
class Mycompare implements Comparator<Student>
{
        public int compare(Student s1,Student s2)
        {
                //先判断成绩,按成绩由高到低排序
                if (s2.getScore() > s1.getScore())
                        return 1;
               
                else if (s2.getScore() == s1.getScore())
                        //如果成绩相同,再判断名字和年龄,都相同为同一人;同成绩不同人的排名不分先后(按照添加进集合的先后排序)
                        return (s1.getName().equals(s2.getName()) && s1.getAge()==s2.getAge())? 0 : 1 ;
               
                else
                        return -1;
        }
}

评分

参与人数 1技术分 +1 收起 理由
殇_心。 + 1 不错。

查看全部评分

回复 使用道具 举报
其实题目已经把思路给你了啊,你就按照题目一步步走代码就行。
先创建学生类,把属性都整好,而一般思路就是属性私有化,然后添加公有set和get方法用于属性赋值和获取。
而排序题目给你提示了,用TreeSet和Comparator,就建立TreeSet集合添加学生对象,建立比较器定义一个比较方法。
回复 使用道具 举报
风乐 发表于 2013-5-11 12:35
import java.util.*;

public class Test10

一眼看下来,你这注释看着特爽!
回复 使用道具 举报
你是怎么学习的啊
回复 使用道具 举报
好的,谢谢您啊
回复 使用道具 举报
   你学到这里应该可以敲出来这个代码的吧   下面是写的 发给你看看  我写的数据流那个我没关 你自己看着关吧   没想到有人要问这个问题  我写的时候也就没加注释 看的话自己看吧
/*
有五个学生,每个学生有三门成绩。
从键盘输入以上数据(包括姓名,三门成绩)
并把学生信息和计算出的总分数高低顺序存放在磁盘文件("Stud.txt")文件中
*/
import java.util.*;
import java.io.*;


public class TestStudent{
        
        
        public static void main(String[]args)throws IOException{
                TreeSet<Student> ts = new TreeSet<Student>();
                ts.add(createStudent());
                ts.add(createStudent());
                ts.add(createStudent());
                ts.add(createStudent());
                ts.add(createStudent());
                for(Student p: ts){
                        System.out.println(p.getName()+"::"+p.jisuan());        
                }
                chucun(ts);
        }        
        
        public static void chucun(TreeSet<Student> ts)throws IOException{
                File f = new File("Stud.txt");
                if( !f.exists()) f.createNewFile();
                BufferedWriter bs = new BufferedWriter(new FileWriter(f));
                /*BufferedOutputStream bos =
                                                                                                new BufferedOutputStream(new FileOutputStream("Stud.txt"));
                Iterator<Student> it = ts.iterator();*/
                Iterator<Student>it = ts.iterator();
                Student s = null;
                while(it.hasNext()){
                                s  = it.next();
                                bs.write(s.getName());
                                bs.write(s.getMath()+"");
                                bs.write(s.getEnglish()+"");
                                bs.write(s.getChina()+"");
                                bs.newLine();
                                bs.flush();
                                
                        }
                bs.close();
        }
        
        public static Student createStudent()throws IOException{
                BufferedReader bs = null;
                String s = null;
                int m = 0;
                int e = 0;
                int c = 0;
                Student st= null;
                try{
                                bs = new BufferedReader(new InputStreamReader(System.in));
                          System.out.println("请输入学生姓名");
                          s = bs.readLine();
                          System.out.println("请输入学生数学成绩");
                          m = Integer.parseInt(bs.readLine());
                          System.out.println("请输入学生英语成绩");
                          e = Integer.parseInt(bs.readLine());
                                System.out.println("请输入学生语文成绩");
                                c = Integer.parseInt(bs.readLine());
                                st = new Student(s,m,c,e);
                                System.out.println(st.getName()+"::"+st.getMath()+"::"+st.getEnglish()+"::"+st.getChina()+"::"+st.jisuan());
                                //return st;
                }catch(IOException er){
                        System.out.println(er.toString());
                }               
               
                return st;
        }
}

class Student implements Comparable<Student>{
        private String name;
        private int m, e, c;
        int sum = 0;

        Student(String name, int m, int c, int e){
                this.name = name;
                this.m = m;
                this.c = c;
                this.e = e;
        }
        
        public int compareTo(Student s1){
                 return -(this.jisuan() - s1.jisuan());
        }
        public void setName(String name){
                this.name = name;
        }

        public void setMath(int m){
                this.m = m;
        }

        public void setEnglish(int e){
                this.e = e;
        }

        public void setChina(int c){
                this.c = c;
        }

        public String getName(){
                return name;
        }

        public int getMath(){
                return m;
        }

        public int getEnglish(){
                return e;
        }

        public int getChina(){
                return c;
        }
        
        public int jisuan(){
                sum = m+e+c;
                return sum;
        }
}

评分

参与人数 1技术分 +1 收起 理由
殇_心。 + 1

查看全部评分

回复 使用道具 举报
如果问题已解决,请及时修改分类,否则继续提问,谢谢合作!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马