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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 小陈期待逆袭 中级黑马   /  2013-5-16 13:32  /  1467 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 小陈期待逆袭 于 2013-5-16 15:33 编辑

我写了一段代码,给五个学生按照分数排序,编译可以通过,运行时,一直在等待。。求大神帮忙分析一下
public class Test10 {

        /**
         * @param args
         */
        public static void main(String[] args) {
                TreeSet<Student> ts = new TreeSet<Student>(new MyComparator());
                ts.add(new Student("Jim",16,98.2));
                ts.add(new Student("Tina",16,100.0));
                ts.add(new Student("Tom",17,93.5));
                ts.add(new Student("Jack",15,88.6));
                ts.add(new Student("Jay",16,98.2));
               
                Iterator<Student> it=ts.iterator();
                while(it.hasNext());
                {
                        Student s=it.next();
                        System.out.println("name:"+s.getName()+" age:"+s.getAge()+" Score:"+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;
               
        }
        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 double getScore() {
                return score;
        }
        public void setScore(double score) {
                this.score = score;
        }
}
class MyComparator implements Comparator<Student>
{

        public int compare(Student s1, Student s2) {
               
                if( s1.getScore() >(s2.getScore()))
                        return 1;
                if(s1.getScore()==s2.getScore())
                        return 0;
                return -1;
        }
        
        }

评分

参与人数 1技术分 +1 收起 理由
袁梦希 + 1

查看全部评分

6 个回复

倒序浏览
while(it.hasNext());后面的分号去掉
回复 使用道具 举报
很郁闷,试了半天,竟然是这个问题。仔细检查下你的代码。小错误,大问题啊。
Iterator<Student> it=ts.iterator();
                while(it.hasNext());//这里
                {
                        Student s=it.next();
                        System.out.println("name:"+s.getName()+" age:"+s.getAge()+" Score:"+s.getScore());
                }

评分

参与人数 1技术分 +1 收起 理由
袁梦希 + 1

查看全部评分

回复 使用道具 举报
我也经常犯这种错误,查出来的时候,差点吐血
回复 使用道具 举报
用eclipse编译这样的错误就会被提醒了。 这道题正好是我的基础测试题,我还没学到这呢,
回复 使用道具 举报
李志敏 发表于 2013-5-16 14:24
while(it.hasNext());后面的分号去掉

谢谢了 我真的吐血了
回复 使用道具 举报
王靖远 发表于 2013-5-16 14:50
用eclipse编译这样的错误就会被提醒了。 这道题正好是我的基础测试题,我还没学到这呢, ...

这个eclipse还真没有提醒 :L
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马