java 实现成绩排序

题目要求:
定义一个学生类;
包括学生学号 姓名 语数外成绩
要求输入5个学生信息
按语数外成绩总和排序输出学生信息

public class Student {

    String name;
        String sno;
        int Chinese;
        int Math;
        int English;

    public Student(String name, String sno, int chinese, int math, int english) {
        this.name = name;
        this.sno = sno;
        Chinese = chinese;
        Math = math;
        English = english;

    }

    public int total(){
        return this.Chinese+this.Math+this.English;
    }

    public void toPrint(){
        System.out.println(this.name+" "+this.sno+"  语文:"+this.Chinese+"  数学:"+this.Math+"  英语:"+this.English);
    }


}
import java.util.*;
public class StudentTest {
    public static void main(String[] args) {
        Student a1 = new Student("小红","001",85,66,99);
        Student a2 = new Student("小明","002",84,78,56);
        Student a3 = new Student("王强","003",46,89,72);
        Student a4 = new Student("李华","004",78,96,80);
        Student a5 = new Student("笑笑","005",92,91,65);

       Student[] ls = new Student[5];
       ls[0]=a1;
       ls[1]=a2;
       ls[2]=a3;
       ls[3]=a4;
       ls[4]=a5;


       for(int i =0;i<5;i++){
           for(int j = 0;j<5-i-1;j++){
               Student temp;
               if(ls[j].total()<ls[j+1].total()){
                   temp=ls[j];
                   ls[j]=ls[j+1];
                   ls[j+1]=temp;
               }
           }
       }


        for ( Student s1:ls) {  //把数组里的每一个对像都赋值给s1
            s1.toPrint();
        }



    }
}

  • 0
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值