需要学习的知识
1.容器List
2.String的split方法
3.包装器类Integer
在这里插入代码片
package homework;
import java.util.*;
class Subject{
String name;
int score;
public Subject(String name, int score) {
this.name = name;
this.score = score;
}
}
class Student implements Comparable<Student>{
String name,id;
int idexofsub;
Subject []a;
public Student(String name,String id,int score,String nameofsub)
{
idexofsub=0;
this.name=name;
this.id=id;
a=new Subject[10];
a[idexofsub]=new Subject(nameofsub,score);
idexofsub++;
}
public void add(String name,int score)
{
a[idexofsub]=new Subject(name,score);
idexofsub++;
}
public double averageScore()
{
double r=0;
for (int i=0;i<idexofsub;i++)
{
r+=a[i].score;
}
return r/idexofsub;
}
@Override
public int compareTo(Student o) {
if (this.averageScore()!=o.averageScore())
{
if(this.averageScore()>o.averageScore())
return -1;
else
return 1;
}
else
{
return (Integer.parseInt(id)-Integer.parseInt(o.id));
}
}
}
public class ListMain1 {
public static void main(String []args)
{
Scanner cin=new Scanner(System.in);
List<Student> L=new ArrayList<Student>();
String []msg=new String[5];
String s;
while (cin.hasNext())
{
s=cin.nextLine();
if (s.equals("exit"))
break;
msg=s.split(",");
boolean flag=false;
int i;
for ( i=0;i<L.size();i++)
{
if (L.get(i).id.equals(msg[1]))
{
flag=true;
break;
}
}
if (flag)
{
L.get(i).add(msg[0], Integer.parseInt(msg[3]));
}
else
{
L.add(new Student(msg[0],msg[1],Integer.parseInt(msg[3]),msg[2]));
}
}
L.sort(null);
for (int i=0;i<L.size();i++)
{
System.out.println("No"+(i+1)+":"+L.get(i).id+","+L.get(i).name);
}
cin.close();
}
}