学生成绩,年龄排序实现

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接口

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值