黑马程序员技术交流社区

标题: 基础测试中出现的问题 [打印本页]

作者: 遮天    时间: 2014-4-3 10:17
标题: 基础测试中出现的问题
本帖最后由 遮天 于 2014-4-7 09:52 编辑

       这是我基础测试题中的一道题目:声明类Student,包含3个成员变量:name、age、score,创建5个对象装入TreeSet, 按照成绩排序输出结果(考虑成绩相同的问题)。

下面是我做的答案
  1. /**
  2. * 第10题:声明类Student,包含3个成员变量:name、age、score,创建5个对象装入TreeSet,
  3. * 按照成绩排序输出结果(考虑成绩相同的问题)。
  4. *
  5. *
  6. *
  7. *
  8. *
  9. *
  10. * */
  11. import java.util.*;
  12. public class Test10
  13. {

  14. public static void main(String[] args)
  15. {
  16. TreeSet<Student> ts=new TreeSet<Student>();

  17. ts.add(new Student("程序员06",23,89.2));
  18. ts.add(new Student("程序员10",45,96.0));
  19. ts.add(new Student("程序员08",32,98.5));
  20. ts.add(new Student("程序员02",34,96.0)); //成绩相同
  21. ts.add(new Student("程序员05",14,95.5));

  22. Iterator<Student> it=ts.iterator();
  23. while(it.hasNext())
  24. {
  25. Student stu=(Student)it.next();
  26. System.out.println(stu.getName()+"******"+stu.getAge()+"******"+stu.getscore());
  27. }

  28. }

  29. }

  30. class Student implements Comparable //该接口强制让学生具备比较性
  31. <Object>
  32. {
  33. private String name;
  34. private int age;
  35. private double score;

  36. Student(String name,int age,double score)
  37. {
  38. this.name=name;
  39. this.age=age;
  40. this.score=score;
  41. }

  42. public int compareTo(Object obj)
  43. {
  44. if(!(obj instanceof Student))
  45. throw new RuntimeException("不是学生类型");
  46. Student s=(Student)obj;

  47. if(this.score<s.score)
  48. return 1;
  49. if(this.score==s.score)
  50. {
  51. //成绩相同时比较姓名
  52. return this.age.compareTo(s.age);
  53. }
  54. return -1;
  55. }

  56. public String getName()
  57. {
  58. return name;
  59. }

  60. public int getAge()
  61. {
  62. return age;
  63. }

  64. public double getscore()
  65. {
  66. return score;
  67. }
  68. }
复制代码


本来是想要在出现成绩相同的情况下,比较年龄
  1. if(this.score==s.score)
  2. {
  3. //成绩相同时比较年龄
  4. return this.age.compareTo(s.age);
  5. }
复制代码

结果提示就报错了,那么代码如何写能比较年龄呢?为什么?


cw.jpg (32.55 KB, 下载次数: 30)

成绩相同时比较年龄的错误提示

成绩相同时比较年龄的错误提示

作者: 年轻的老头    时间: 2014-4-3 10:45
this.age.compareTo(s.age); 改为:return new Integer(this.age).compareTo(new Integer(s.age));   compareTo只适合实现了Comparable的对象类型
作者: 李军辉    时间: 2014-4-3 10:45
本帖最后由 李军辉 于 2014-4-3 10:48 编辑


if(this.score<s.score)
return 1;
if(this.score==s.score)
{
//成绩相同时比较姓名
return (this.age-s.age);//相减
}
return -1;
}

作者: 闲人    时间: 2014-4-3 10:49
compareTo用于比较引用数据类型,你的age是int型,直接用> == <,比较就好了
或者把age定义成对象也可以
作者: H-Deka    时间: 2014-4-3 10:50
return this.age.compareTo(s.age);

修改为 return this.age-s.age;
作者: Engle    时间: 2014-4-3 12:21
你的代码:
if(this.score==s.score)
{
//成绩相同时比较年龄
return this.age.compareTo(s.age);
}
报错的原因是你的age定义的是int类型的,注意基本数据类型是没有compareTo()方法的
最简单的修改,把你的age改为int的包装类Integer类型

只需要改这一行代码:
private int age;
改为private Integer age;

作者: 黄晓鑫    时间: 2014-4-3 13:17
==整数是用==
作者: 小班。    时间: 2014-4-3 13:42
  1. package cn.com.problem;

  2. import java.util.*;

  3. public class ggg {

  4.         public static void main(String[] args) {
  5.                 TreeSet<Student> ts = new TreeSet<Student>();

  6.                 ts.add(new Student("程序员06", 23, 89.2));
  7.                 ts.add(new Student("程序员10", 45, 96.0));
  8.                 ts.add(new Student("程序员08", 32, 98.5));
  9.                 ts.add(new Student("程序员02", 34, 96.0)); // 成绩相同
  10.                 ts.add(new Student("程序员05", 14, 95.5));

  11.                 Iterator<Student> it = ts.iterator();
  12.                 while (it.hasNext()) {
  13.                         Student stu = (Student) it.next();
  14.                         System.out.println(stu.getName() + "******" + stu.getAge()
  15.                                         + "******" + stu.getscore());
  16.                 }

  17.         }

  18. }

  19. class Student implements Comparable // 该接口强制让学生具备比较性
  20.                 <Object> {
  21.         private String name;
  22.         private int age;
  23.         private double score;

  24.         Student(String name, int age, double score) {
  25.                 this.name = name;
  26.                 this.age = age;
  27.                 this.score = score;
  28.         }

  29.         public int compareTo(Object obj) {
  30.                 if (!(obj instanceof Student))
  31.                         throw new RuntimeException("不是学生类型");
  32.                 Student s = (Student) obj;

  33.                 if (this.score < s.score)
  34.                         return 1;
  35.                 if (this.score == s.score) {
  36.                         // 成绩相同时比较姓名
  37. //                        return this.age.compareTo(s.age);
  38.                         //Cannot invoke compareTo(int) on the primitive type int这是在将鼠标放在上面那句话时,出现的错误。
  39.                         //你好好跟一下就知道问题所在了。invoke是调用方法的意思。错误的原因是:不能再基础类型int上调用compareTo方法与基本类型int比较
  40.                         //所以解决发放很明显,你int强制转换成一个不是int类型的就可以了,比如说Integer、String都行
  41.                         //        return new Integer(this.age).compareTo(new Integer(s.age));//这种写法也可以
  42.                         return new String(String.valueOf(this.age)).compareTo(String.valueOf(s.age));//百度一下,就能看见compareTo用于字符串的字符顺序比较。
  43.                         //所以将其换成字符串
  44.                 }
  45.                 return -1;
  46.         }

  47.         public String getName() {
  48.                 return name;
  49.         }

  50.         public int getAge() {
  51.                 return age;
  52.         }

  53.         public double getscore() {
  54.                 return score;
  55.         }
  56. }
复制代码

作者: 戒爱    时间: 2014-4-3 13:44
comperTo只能比较一些对象,而int和Integer区别恰好就在这里
作者: 一年_Hei    时间: 2014-4-3 14:52
把int型包装成Integer型,还有加了泛型,迭代就不用强转了,看了不自在




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2