import java.util.*;
public class ArraysTest
{
public static void main(String args[])
{
Student []stu=new Student[5];
stu[0]=new Student(23,91.5,"Lily");
stu[1]=new Student(22,90,"John");
stu[2]=new Student(20,92,"Mike");
stu[3]=new Student(21,90,"Lucy");
stu[4]=new Student(22,91.5,"WenXi");
for(int i=0;i<stu.length;i++)
for(int j=0;j<stu.length-i-1;j++)
{ if(stu[j].score<stu[j+1].score)
{
Student a=stu[j];
stu[j]=stu[j+1];
stu[j+1]=a;
}
}
for(int i=0;i<stu.length-1;i++)
{
if(stu[i].score==stu[i+1].score)
{
if(stu[i].age<stu[i+1].age)
{
Student a=stu[i];
stu[i]=stu[i+1];
stu[i+1]=a;
}
}
}
for(int i=0;i<stu.length;i++)
System.out.println("姓名:"+stu[i].name+", 年龄: "+stu[i].age+", 成绩: "+stu[i].score);
}
}
class Student
{
int age;
double score;
String name;
public Student(int age,double score,String name)
{
this.age=age;
this.score=score;
this.name=name;
}
}
//Arrays.sort(Object[] a);
//能够实现对象数组的排序,但是本程序中的排序无法实现,其没有实现Comparable接口
学生成绩,年龄排序实现
最新推荐文章于 2022-05-18 22:56:26 发布