/*
希望在学生毕业的时候统计出学生在校期间考试成绩的排名,
写一个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 错误
请帮忙指点一下.
希望在学生毕业的时候统计出学生在校期间考试成绩的排名,
写一个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 错误
请帮忙指点一下.
解决方案 »
- jni FindClass返回null
- 程序中有一条语句,想让它5秒钟之后,执行不知道是否可以。。
- 关闭一个界面的同时,如何打开另一个界面??
- 实例编程解答打印正弦函数
- 为什么我的java在dos下程序运行时,老是显示error:can not read ave.java.ave是我自己写的程序名,环境变量我也设置对了呀,运行java -version一切正常
- 两种写法效率一样吗
- 关于JAVA中继承的SUPER问题
- 如何写ftp程序
- com.jspsmart.upload.SmartUpload类的问题!
- java的问题(应用领域的问题)
- 请教怎么解决这个问题jmagick.dll: Can't find dependent libraries
- Connection没有关闭呀,为什么出现java.sql.SQLException: ResultSet is closed?
楼上说的在List<Kecheng>只能加入Kecheng也是一个原因总结出来你的原因,把Course改成Kecheng都行了
List li=new ArrayList();
这样就不会进行类型检查了,你可以向里面add任何Object这样时候你再取li中的元素的时候就需要进行强制类型转换了。
还有,楼主的
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)));
这一段,应该写在方法里
类中只能声明变量,变量的操作,应该放在方法中~
sigh.
就把名字换个土一点的.(一方水土养一方人)
原来的我注释掉了.
可是,还是类似的问题.
没办法,我只有到这个问了.去掉了注释,可是忘了,其他有些我也变动了.
看了您们的帖子.
觉得还是 v38(国产磁悬浮拖拉机:不上CSDN有多年) 说道了问题的要害.
我该把操作,放在方法内!!!
修改后的代码:
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;
}
}