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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© ぺsimon☆ 中级黑马   /  2013-5-12 22:22  /  1811 人查看  /  8 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 ぺsimon☆ 于 2013-5-13 19:30 编辑
  1. 注意:问题是,不知道为什么在dos命令行里编译没有出错,把源文件复制到Eclipse上编译也没有出错,但是在Eclipse上面怎么会有叹号呢? 帮帮忙把兄弟们
  2. package com.itheima;
  3. /**
  4. 第十题: 定义一个学生类, 需要有姓名, 年龄, 考试成绩三个成员属性,创建5个对象, 属性可为任意值. 编程对这5个对象按成绩排序,并将结果输出。(提示,用TreeSet和Comparator实现)
  5. */

  6. import java.util.*;
  7. import java.io.*;
  8. class Student implements Comparable<Student>//定义一个学生类并继承Comparable类,让学生类具备比较性
  9. {
  10.         //定义学生的姓名,年龄和分数
  11.         private String name;
  12.         private int age;
  13.         private double score;


  14.         //构造函数初始化,把学生的姓名,年龄和分数赋值给学生对象
  15.         Student(String name,int age,double score)
  16.         {
  17.         this.name=name;
  18.         this.age=age;
  19.         this.score=score;
  20.         }

  21.         //设置学生的姓名
  22.         public void setName(String name)
  23.         {
  24.         this.name=name;
  25.         }

  26.         //获取学生的姓名
  27.         public String getName()
  28.         {
  29.         return this.name;
  30.         }

  31.         //设置学生的年龄
  32.         public void setAge(int age)
  33.         {
  34.         this.age=age;
  35.         }

  36.         //获取学生的年龄
  37.         public int getAge()
  38.         {
  39.         return this.age;
  40.         }

  41.         //设置学生的分数
  42.         public void setScore(double score)
  43.         {
  44.         this.score=score;
  45.         }

  46.         //获取学生的分数
  47.         public double getScore()
  48.         {
  49.         return this.score;
  50.         }


  51.         //覆盖Comparable类的compareTo方法,让学生有一个自然的排序状态
  52.         public int compareTo(Student stu)
  53.         {
  54.         //用变量num记录学生按照姓名比较后的结果
  55.         int num=this.name.compareTo(stu.name);

  56.         //如果姓名相同,再比较学生的年龄
  57.         if(num==0)
  58.         return new Integer(this.age).compareTo(new Integer(stu.age));

  59.         return num;
  60.         }



  61.         //覆盖hashCode方法,以便于这个类被集合使用
  62.         public int hashCode()
  63.         {
  64.         return name.hashCode()+(age*37);
  65.         }


  66.         //覆盖equals方法,以便于这个类被集合使用
  67.         public boolean equals(Object obj)
  68.         {
  69.         //判断obj是否Student的子类对象
  70.         if(!(obj instanceof Student))
  71.         throw new RuntimeException("类型不匹配");

  72.         Student stu=(Student)obj;
  73.         return this.name.equals(stu.name)&&this.age==stu.age;
  74.         }


  75.         //覆盖toString方法
  76.         public String toString()
  77.         {
  78.         return "Student["+name+", "+age+", "+score+"]";
  79.         }
  80. }

  81. //自定义一个比较器,按照学生的分数从低到高排序
  82. class StuCom implements Comparator<Student>
  83. {
  84.         //覆盖compare方法
  85.         public int compare(Student s1,Student s2)
  86.         {

  87.         //用变量num记录学生分数比较后的结果
  88.         int num=new Double(s1.getScore()).compareTo(new Double(s2.getScore()));

  89.         if(num==0)
  90.         return s1.getName().compareTo(s2.getName());

  91.         return num;
  92.         }
  93. }



  94. class Test10
  95. {
  96.         public static void main(String[] args)
  97.         {

  98.         //定义一个TreeSet集合存放Student对象,因为分数一般是从高到底进行排序所以用了Collections的强        行逆转比较器的方法

  99.         TreeSet<Student> ts=new TreeSet<Student>(Collections.reverseOrder(new StuCom()));

  100.         //向集合添加Student对象
  101.         ts.add(new Student("zhangsan01",32,100));
  102.         ts.add(new Student("zhangsan02",32,82));
  103.         ts.add(new Student("zhangsan03",32,83));
  104.         ts.add(new Student("zhangsan04",32,48));
  105.         ts.add(new Student("zhangsan05",32,55));

  106.         //利用高级for循环打印集合
  107.         for(Student stu : ts)
  108.         {
  109.         System.out.println(stu);
  110.         }
  111.         }
  112. }
复制代码

11.jpg (15.52 KB, 下载次数: 1)

11.jpg

评分

参与人数 1技术分 +1 收起 理由
田磊阳 + 1

查看全部评分

8 个回复

倒序浏览
这个问题应该比较简单的。顶上去 让别人关注到 我还不知道为什么
回复 使用道具 举报
基友,帮顶!
回复 使用道具 举报
  1. package cn.itcast.day02;



  2. //package com.itheima;

  3. /**

  4. 第十题: 定义一个学生类, 需要有姓名, 年龄, 考试成绩三个成员属性,创建5个对象, 属性可为任意值. 编程对这5个对象按成绩排序,并将结果输出。(提示,用TreeSet和Comparator实现)

  5. */


  6. import java.util.*;

  7. import java.io.*;

  8. class Student implements Comparable<Student>//定义一个学生类并继承Comparable类,让学生类具备比较性

  9. {

  10.         //定义学生的姓名,年龄和分数

  11.         private String name;

  12.         private int age;

  13.         private double score;



  14.         //构造函数初始化,把学生的姓名,年龄和分数赋值给学生对象

  15.         Student(String name,int age,double score)

  16.         {

  17.         this.name=name;

  18.         this.age=age;

  19.         this.score=score;

  20.         }


  21.         //设置学生的姓名

  22.         public void setName(String name)

  23.         {

  24.         this.name=name;

  25.         }


  26.         //获取学生的姓名

  27.         public String getName()

  28.         {

  29.         return this.name;

  30.         }


  31.         //设置学生的年龄

  32.         public void setAge(int age)

  33.         {

  34.         this.age=age;

  35.         }


  36.         //获取学生的年龄

  37.         public int getAge()

  38.         {

  39.         return this.age;

  40.         }


  41.         //设置学生的分数

  42.         public void setScore(double score)

  43.         {

  44.         this.score=score;

  45.         }


  46.         //获取学生的分数

  47.         public double getScore()

  48.         {

  49.         return this.score;

  50.         }



  51.         //覆盖Comparable类的compareTo方法,让学生有一个自然的排序状态

  52.         public int compareTo(Student stu)

  53.         {

  54.         //用变量num记录学生按照姓名比较后的结果

  55.         int num=this.name.compareTo(stu.name);


  56.         //如果姓名相同,再比较学生的年龄

  57.         if(num==0)

  58.         return new Integer(this.age).compareTo(new Integer(stu.age));


  59.         return num;

  60.         }




  61.         //覆盖hashCode方法,以便于这个类被集合使用

  62.         public int hashCode()

  63.         {

  64.         return name.hashCode()+(age*37);

  65.         }



  66.         //覆盖equals方法,以便于这个类被集合使用

  67.         public boolean equals(Object obj)

  68.         {

  69.         //判断obj是否Student的子类对象

  70.         if(!(obj instanceof Student))

  71.         throw new RuntimeException("类型不匹配");


  72.         Student stu=(Student)obj;

  73.         return this.name.equals(stu.name)&&this.age==stu.age;

  74.         }



  75.         //覆盖toString方法

  76.         public String toString()

  77.         {

  78.         return "Student["+name+", "+age+", "+score+"]";

  79.         }

  80. }


  81. //自定义一个比较器,按照学生的分数从低到高排序

  82. class StuCom implements Comparator<Student>

  83. {

  84.         //覆盖compare方法

  85.         public int compare(Student s1,Student s2)

  86.         {


  87.         //用变量num记录学生分数比较后的结果

  88.         int num=new Double(s1.getScore()).compareTo(new Double(s2.getScore()));


  89.         if(num==0)

  90.         return s1.getName().compareTo(s2.getName());


  91.         return num;

  92.         }

  93. }




  94. class Test10

  95. {

  96.         public static void main(String[] args)

  97.         {


  98.         //定义一个TreeSet集合存放Student对象,因为分数一般是从高到底进行排序所以用了Collections的强        行逆转比较器的方法


  99.         TreeSet<Student> ts=new TreeSet<Student>(Collections.reverseOrder(new StuCom()));


  100.         //向集合添加Student对象

  101.         ts.add(new Student("zhangsan01",32,100));

  102.         ts.add(new Student("zhangsan02",32,82));

  103.         ts.add(new Student("zhangsan03",32,83));

  104.         ts.add(new Student("zhangsan04",32,48));

  105.         ts.add(new Student("zhangsan05",32,55));


  106.         //利用高级for循环打印集合

  107.         for(Student stu : ts)

  108.         {

  109.         System.out.println(stu);

  110.         }

  111.         }

  112. }
复制代码
哥们.没有错啊

运行结果

Student[zhangsan01, 32, 100.0]
Student[zhangsan03, 32, 83.0]
Student[zhangsan02, 32, 82.0]
Student[zhangsan05, 32, 55.0]
Student[zhangsan04, 32, 48.0]
回复 使用道具 举报
噢噢噢.注意下.所在的包.和类名字是否相同吧

评分

参与人数 1技术分 +1 收起 理由
田磊阳 + 1

查看全部评分

回复 使用道具 举报
谢谢各位兄弟啦,原来是同一个包里,类名重复了,我把类名改了之后,没有出错了,但是为什么Eclipse里这个类上面有个叹号的?是代表有错误吗
回复 使用道具 举报
孙百鑫 发表于 2013-5-12 22:33
哥们.没有错啊

运行结果

谢谢各位兄弟啦,原来是同一个包里,类名重复了,我把类名改了之后,没有出错了,但是为什么Eclipse里这个类上面有个叹号的?是代表有错误吗
回复 使用道具 举报
山西_李帅 来自手机 中级黑马 2013-5-13 01:39:37
8#
ぺsimon☆ 发表于 2013-5-12 22:48
谢谢各位兄弟啦,原来是同一个包里,类名重复了,我把类名改了之后,没有出错了,但是为什么Eclipse里这个类上面 ...

不是,这个正常。也可以将这个感叹号去掉,明天用电脑上的时候给你说下怎么去掉。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马