class Student
{
String name;
double math;
double english;
double comp;
double ave;
public void setAve()
{
this.ave=(this.math+this.english+this.comp)/3;
}
}
public class Person
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
List<Student> list=new ArrayList<Student>();
for(int n=1;n<=2;n++)
{
Student temp=new Student();
System.out.println("請輸入第"+n+"個學生的姓名:");
temp.name=sc.nextLine();
System.out.println("請輸入第"+n+"個學生的數學成績:");
temp.math=sc.nextInt();
System.out.println("請輸入第"+n+"個學生的英語成績:");
temp.english=sc.nextInt();
System.out.println("請輸入第"+n+"個學生的計算機成績:");
temp.comp=sc.nextInt();
sc.nextLine();
temp.setAve();
list.add(temp);
System.out.println();
}
Collections.sort(list, new Comparator<Student>()//排序
{
public int compare(Student o1,Student o2)
{
if(o1.ave!=o2.ave)//平均成績從高到低
{
return (int)(-Math.signum(o1.ave-o2.ave));
}
else if(o1.math!=o2.math)//平均成績相同按數學從低到高
{
return (int)(Math.signum(o1.math-o2.math));
}
else if(o1.english!=o2.english)//平均成績,數學成績相同按英語從低到高
{
return (int)(Math.signum(o1.english-o2.english));
}
else
{
return (int)(Math.signum(o1.comp-o2.comp));
}
}
});
System.out.println("姓名\t數學\t英語\t計算機\t平均成績");
Iterator<Student> it=list.iterator();
while(it.hasNext())
{
Student temp=it.next();
System.out.println (temp.name+"\t"+temp.math+"\t"+temp.english+"\t"+temp.comp+"\t"+temp.ave);
}
}
}