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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 风云 中级黑马   /  2013-5-21 18:31  /  1849 人查看  /  8 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

import java.util.*;
class StudentDemo
{
        public static void main(String[] args)
        {
          Set<student> tr=new TreeSet<student>(new Comparator());
             tr.add(new student("孙三",18,87));
                 tr.add(new student("李四",19,86));
                 tr.add(new student("周五",18,77));
                 tr.add(new student("赵一",19,47));
                 tr.add(new student("钱二",18,85));
                 for(Student s:tr)
                {
                         System.out.println(s);
                }
        }
}

class Student implements Comparator<Student>
{
        public string name;
        public int age;
        public int score;
        Student(string name,int age,int score)
        {
                 this.name=name;
                 this.age=age;
                 this.score=score;
        }       
        public int Compare(Student s1)
        {
              if(this.score()>s1.score())
                  {
                          return 1;
                  if(s1.score()<this.score())
                          return -1;
                      return 0;
                  }
        }

}   
运行的时候总是提示public int Compare(Student s1)这个地方出错呢?

评分

参与人数 1技术分 +1 收起 理由
袁梦希 + 1 加油风云 不错

查看全部评分

8 个回复

倒序浏览
你那个括号是中文字符的括号。。。
回复 使用道具 举报
本帖最后由 李慧声 于 2013-5-21 19:03 编辑
  1. class StudentDemo
  2. {
  3.         public static void main(String[] args)
  4.         {
  5.                 Set<Student> tr=new TreeSet<Student>(/*new MyComparator()*/);
  6.                 tr.add(new Student("孙三",18,87));
  7.                 tr.add(new Student("李四",19,86));
  8.                 tr.add(new Student("周五",18,77));
  9.                 tr.add(new Student("赵一",19,47));
  10.                 tr.add(new Student("钱二",18,85));
  11.                 for(Student s:tr)
  12.                 {
  13.                         System.out.println(s);
  14.                 }
  15.         }
  16. }

  17. class MyComparator implements Comparator<Student> {

  18.          public int compare(Student s1,Student s2) {
  19.                  if(s1.score > s2.score)
  20.                          return -1;
  21.                  else if(s1.score < s2.score)
  22.                          return 1;
  23.                  else
  24.                          return 0;
  25.         }
  26. }
  27. class Student implements Comparable
  28. {
  29.         public String name;
  30.         public int age;
  31.         public int score;
  32.         Student(String name,int age,int score)
  33.         {
  34.                 this.name=name;
  35.                 this.age=age;
  36.                 this.score=score;
  37.         }        
  38.         @Override
  39.         public String toString() {
  40.                 return "Student [name=" + name + ", age=" + age + ", score=" + score
  41.                                 + "]";
  42.         }
  43.         @Override
  44.         public int compareTo(Object obj) {
  45.                 if(obj instanceof Student) {
  46.                         Student stu = (Student)obj;
  47.                         if(this.score < stu.score)
  48.                                 return 1;
  49.                         else if(this.score > stu.score)
  50.                                 return -1;
  51.                         else
  52.                                 return 0;
  53.                 }
  54.                 return 0;
  55.         }
  56. }   
复制代码
你的程序代码中错误比较多,拼写错误String s没有大写 Student s没有大写等等,成员引用错误,score不是方法,不能用score(),基础很重要哦。
比较有两种方法,自身实现Comparable,实现compareTo方法;另外一种另外写一个比较方法实现 Comparator<T>实现compare(T t1 ,T t2),这是我写的两种方法,你参考一下。





评分

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

查看全部评分

回复 使用道具 举报
还有好多string要改成String。。。
回复 使用道具 举报
我把你的代码复制下来执行后,发现错误老多了。例如括号是中文的了,你建立对象时,用的new student() student应该开头大写
我把你的程序大改了一下,不懂可以继续问
package com.sdut.day2;
import java.util.Comparator;
import java.util.TreeSet;
import java.util.Iterator;
class  Student{
  String name;
  int age;
  float score;
public Student(String name, int age, float score) {
  super();
  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 float getScore() {
  return score;
}

public void setScore(float score) {
  this.score = score;
}

@Override
public String toString() {
  return "Student [name=" + name + ", age=" + age + ", score=" + score
    + "]";
}
  static class StudentCompare implements Comparator {

@Override
  public int compare(Object o1, Object o2) {
   Student s1 = (Student) o1;
   Student s2 = (Student) o2;
   int result = s1.score> s2.score?1:(s1.score==s2.score?0:-1);
   if(result == 0){
    result = s1.name.compareTo(s2.name);
   }
   return result;
  }
   
  }

}
public class Test {
public static void main(String[]args){
TreeSet tr = new TreeSet(new Student.StudentCompare());
tr.add(new Student("孙三",18,87));
tr.add(new Student("李四",19,86));
tr.add(new Student("周五",18,77));
tr.add(new Student("赵一",19,47));
tr.add(new Student("钱二",18,85));
Iterator it = tr.iterator();
while(it.hasNext()){
  System.out.println(it.next());
}
}
}

评分

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

查看全部评分

回复 使用道具 举报
李慧声 发表于 2013-5-21 18:57
你的程序代码中错误比较多,拼写错误String s没有大写 Student s没有大写等等,成员引用错误,score不是方 ...

答得非常好
回复 使用道具 举报
山鹰 中级黑马 2013-5-21 19:14:39
7#
在黑马训练营中的培训编程用的是那个工具  EditPlus还是MyEclipse呢  MyEclipse用惯了  没有用EditPlus写过JAVA 程序
回复 使用道具 举报
黄玉昆 黑马帝 2013-5-21 19:38:21
8#
你这里有太多的错误,我把正确的写一下。
  1. import java.util.*;

  2. public class StudentDemo {
  3.         public static void main(String[] args) {
  4.                 Set<Student> tr = new TreeSet<Student>();
  5.                 tr.add(new Student("孙三", 18, 87));
  6.                 tr.add(new Student("李四", 21, 86));
  7.                 tr.add(new Student("周五", 22, 77));
  8.                 tr.add(new Student("赵一", 23, 47));
  9.                 tr.add(new Student("钱二", 18, 85));
  10.                 for (Student s : tr) {
  11.                         System.out.println(s);
  12.                 }
  13.         }
  14. }

  15. class Student implements Comparable<Student> {
  16.         public String name;
  17.         public int age;
  18.         public int score;

  19.         Student(String name, int age, int score) {
  20.                 this.name = name;
  21.                 this.age = age;
  22.                 this.score = score;
  23.         }

  24.         @Override
  25.         public int compareTo(Student s1) {
  26.                 if (this.score > s1.score)
  27.                         return 1;
  28.                 else if (this.score < s1.score)
  29.                         return -1;
  30.                 return 0;
  31.         }

  32.         public String toString(){
  33.                 return name + "," + age + "," + score;
  34.         }
  35. }
复制代码
建议你好好看看集合的知识。这部分很重要,希望你多看看视频
回复 使用道具 举报
风云 中级黑马 2013-5-22 12:19:26
9#
你写的这个我明白,还有一点比较迷惑,在这里能不能把  Comparable()  直接改为自定义的比较器  Comparetor();
class Student implements Comparetor<Student>
{

        public String name;

       public int age;

        public int score;

       Student(String name, int age, int score)
{

             this.name = name;

               this.age = age;

               this.score = score;

        }

       public int compare(Student s1,Student s2)
{

              if s1.score > s2.score)

                    return 1;

               else if (s1.score < s2.score)

                      return -1;

               return 0;

      }



     public String toString(){

              return name + "," + age + "," + score;

       }

}
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马