java实验题——学生成绩排序并打印

编写一个程序,提示用户输入学生数量、各自的姓名和他们的成绩,并按照成
绩的降序来打印学生的姓名。
import java.util.*;
class StudentInfo{
	private String name;
	private int score;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getScore() {
		return score;
	}
	public void setScore(int score) {
		this.score = score;
	}
	public void sort(StudentInfo[] stu,int count)
	{
		StudentInfo temp;
		for(int i=0;i<count;i++)
		{
			for(int j=0;j<count-i-1;j++)
			{
				if (stu[j].getScore()<stu[j+1].getScore()) 
				{
                   temp=stu[j];
                   stu[j]=stu[j+1];
                   stu[j+1]=temp;
                }
			}
		}
	}
	public void print(StudentInfo[] stu,int count){
		for (int i=0; i<count;i++) {
			System.out.println(stu[i].getName()+" "+stu[i].getScore());
        }
	}
}
public class ScoreSort {
	public static void main(String[] args){
		StudentInfo s=new StudentInfo();
		StudentInfo[] stu= new StudentInfo[20];
        Scanner input= new Scanner(System.in);
        System.out.println("请输入学生个数:");
        int count=input.nextInt();
        int num=0;
        while(num<count) {
        	System.out.println("请输入第"+(num+1)+"个学生的姓名和分数:");
            Scanner scan=new Scanner(System.in);
            String strLine=scan.nextLine();
            String[] strLineArr = strLine.split(" ");
            StudentInfo st = new StudentInfo();
            st.setName(strLineArr[0]);
            st.setScore(Integer.parseInt(strLineArr[1]));
            stu[num]=st;
            num++;
        }
		s.sort(stu,count);
		System.out.println("排序后的学生信息为:");
		s.print(stu,count);
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值