/*
希望在学生毕业的时候统计出学生在校期间考试成绩的排名,
写一个Student类,其中用集合来管理每个学生的各个科目的考试成绩,
将多个Student对象放在集合中,打印出学生的总分以及排名
*/
import java.util.*;
public class SortStudent{  public static void main(String[] args){
     List<Student> l=new ArrayList<Student>();
     l.add(new Student("hlonger",30));
     l.add(new Student("bluelaster",19));
     l.add(new Student("mk",24));
     Collections.sort(l);
     print(l);
  }
  
  public static void print(List<Student> li){
      Iterator<Student> it=li.iterator();
      for(Student s:li)
        System.out.println(s);
  }}class Student implements Comparable <Student>{
  private String name;
  private int age;
 
  Random rd=new Random();
  
  public Student(){}
  public Student(String name,int age){
   this.name=name;
   this.age=age;
  }
  
  List<Kecheng> li=new ArrayList<Kecheng>();   
 
  li.add(new Course("英语",rd.nextInt(100)));
  li.add(new Course("物理",rd.nextInt(100)));
  li.add(new Course("化学",rd.nextInt(100)));
  li.add(new Course("语文",rd.nextInt(100)));
  
  public String toString(){
   return "Student:"+name+" Age:"+age;
  }
  
  public double getSumOfScore(List<Kecheng> li){
      double sum=0.0;
      for(int i=0;i<li.size();i++)
      sum+=li[i].getScore(); 
      return sum;
  }
  
  public int compareTo(Student s){
  return (int)(this.getSumOfScore(this.li)-s.getSumOfScore(s.li));
  }
}
class Kecheng{
    private String cname;
    private double score;
    
    public Kecheng(){}
    public Kecheng(String cname,double score){
      this.cname=cname;
      this.score=score;
    }
    
    public String getCname(){
      return cname;
    }
    public void setCname(String cname){
      this.cname=cname;
    }
    
    public double getScore(){
      return score;
    }
    public void setScore(double score){
      this.score=score;
    }
}
编译出错提示为:
SortStudent.java:40: 需要 <标识符>
          li.add(new Course("英语",rd.nextInt(100)));
                ^
SortStudent.java:41: 需要 <标识符>
          li.add(new Course("物理",rd.nextInt(100)));
                ^
SortStudent.java:42: 需要 <标识符>
          li.add(new Course("化学",rd.nextInt(100)));
                ^
SortStudent.java:43: 需要 <标识符>
          li.add(new Course("语文",rd.nextInt(100)));
                ^
4 错误
请帮忙指点一下.

解决方案 »

  1.   

    你定义的List<Kecheng> li=new ArrayList<Kecheng>(); 要相list中添加的元素是Kecheng类的对象,而你实际添加的却是:li.add(new Course("语文",rd.nextInt(100))); Course对象。
      

  2.   

    这是jdk1.5添加的类型检验特性
      

  3.   

    根据你的定义向li添加的必须是Kecheng类对象或者是Kecheng类的子类的对象
      

  4.   

    你 Course 类导入了吗?有没有这个类,是报你说的错的直接原因
    楼上说的在List<Kecheng>只能加入Kecheng也是一个原因总结出来你的原因,把Course改成Kecheng都行了
      

  5.   

    不存在楼上说的原因,如果是Course类没有导入不是这个错误提示,就是我说的原因,当然还有一种解决方法,就是把类型检查去掉,写成下面的
    List li=new ArrayList();
    这样就不会进行类型检查了,你可以向里面add任何Object这样时候你再取li中的元素的时候就需要进行强制类型转换了。
      

  6.   

    编译的问题高手都来解决,就是你的List放错了对象
      

  7.   

    楼主在外面定义了一个课程类,类名用的是拼音:Kecheng,但是他在Student 类中引用课程类的时候,却使用了课程的英文名Course。编译的时候,会为认为这个类没有~
     
    还有,楼主的
          li.add(new Course("英语",rd.nextInt(100)));
          li.add(new Course("物理",rd.nextInt(100)));
          li.add(new Course("化学",rd.nextInt(100)));
          li.add(new Course("语文",rd.nextInt(100)));
    这一段,应该写在方法里
    类中只能声明变量,变量的操作,应该放在方法中~
      

  8.   

    开始,我是使用Course的,可编译通不过.我怀疑我的Course的合法性!
    sigh.
    就把名字换个土一点的.(一方水土养一方人)
    原来的我注释掉了.
    可是,还是类似的问题.
    没办法,我只有到这个问了.去掉了注释,可是忘了,其他有些我也变动了.
    看了您们的帖子.
    觉得还是  v38(国产磁悬浮拖拉机:不上CSDN有多年) 说道了问题的要害.
    我该把操作,放在方法内!!!
      

  9.   

    问题以解决,结贴.
    修改后的代码:
    import java.util.*;
    public class SortStudent{  public static void main(String[] args){
         
         List<Student> l=new ArrayList<Student>();
         
         Student s1=new Student("hlonger",29);
         s1.putScore();
         
         Student s2=new Student("bluelaster",19);
         s2.putScore();
         
         Student s3=new Student("mk",30);
         s3.putScore();
         
         Student s4=new Student("ixling",20);
         s4.putScore();
         
         
         l.add(s1);
         l.add(s2);
         l.add(s3);
         l.add(s4);
         
         Collections.sort(l);
         print(l);
      }
      
      public static void print(List<Student> li){
          
          for(Student s:li)
            System.out.println(s);
      }}class Student implements Comparable <Student>{
      private String name;
      private int age;
      Random rd=new Random();
      
      
      public Student(){}
      public Student(String name,int age){
       this.name=name;
       this.age=age;
      }
      
      List<Course> li=new ArrayList<Course>();   
     
      public void putScore(){
       li.add(new Course("英语",rd.nextInt(100)));
       li.add(new Course("物理",rd.nextInt(100)));
       li.add(new Course("化学",rd.nextInt(100)));
       li.add(new Course("语文",rd.nextInt(100)));
      } 
      public String toString(){
       return "Student:"+name+" Age:"+age+" TotalScore="+getSumOfScore();
      }
      
      public double getSumOfScore(){
          double sum=0.0;
          for(Course k:this.li)
          sum+=k.getScore(); 
          return sum;
      }
      
      public int compareTo(Student s){
       return (int)(this.getSumOfScore()-s.getSumOfScore());
      }
    }
    class Course{
        private String cname;
        private double score;
        
        public Course(){}
        public Course(String cname,double score){
          this.cname=cname;
          this.score=score;
        }
        
        public String getCname(){
          return cname;
        }
        public void setCname(String cname){
          this.cname=cname;
        }
        
        public double getScore(){
          return score;
        }
        public void setScore(double score){
          this.score=score;
        }
    }