本帖最后由 王博 于 2012-12-25 14:17 编辑
用myeclipse编写完代码编译能通过,也能运行,可是文件前面为什么会有个小叹号啊。。。加了其他文件又变成叉了。。。试了怎么改代码都没办法
下面附图:
下面附上我的代码
package com.itheima import java.util.*; class Test { public static void main(String[] args) { //定义一个TreeSet集合 TreeSet<Student> ts = new TreeSet<Student>(); //添加元素 ts.add(new Student("heima01",22,89)); ts.add(new Student("heima02",20,90)); ts.add(new Student("heima03",19,56)); ts.add(new Student("heima04",19,77)); ts.add(new Student("heima05",20,65));
//迭代器打印元素 Iterator<Student> it = ts.iterator(); while(it.hasNext()) { Student stu = (Student)it.next(); System.out.println("姓名是"+stu.getName()+",年龄:"+stu.getAge()+",成绩:"+stu.getScore()); } } } class Student implements Comparable //错误应该是这里吧
{ 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; } public int compareTo(Object obj) { if(!(obj instanceof Student)) throw new RuntimeException("不是学生对象"); Student s = (Student)obj; if(this.score<s.score) return 1; if(this.score==s.score) { return this.name.compareTo(s.name); } return -1;
} public String getName() { return name; } public int getAge() { return age; } public double getScore() { return score; } } |