Java 练习题-从键盘录入 5 个学生的信息(姓名 语 数 外) 按照总分从高到低输出学生信息

package todayHw;

import java.util.ArrayList;
import java.util.Scanner;

/*
键盘录入 5 个学生的信息(姓名 语 数 外)
按照总分从高到低输出学生信息
 */
public class Todo4 {
    public static void main(String[] args) {


        Student[] arr = new Student[5];
        for (int i = 0; i < arr.length; i++) {
            System.out.println("输入姓名");
            String tempName = new Scanner(System.in).next();
            System.out.println("输入语文成绩");
            double tempCHN = new Scanner(System.in).nextDouble();
            System.out.println("输入数学成绩");
            double tempMath = new Scanner(System.in).nextDouble();
            System.out.println("输入英语成绩");
            double tempENG = new Scanner(System.in).nextDouble();
            arr[i] = new Student(tempName,tempCHN,tempMath, tempENG);
        }

        ArrayList<Score> list = new ArrayList<>();

        for (Student student : arr) {
            list.add(new Score(student.getName(), student.getChinese() + student.getMath() + student.getEnglish()));
        }

       /* for (int i = 0; i < arr.length; i++) {
            list.add(new Score(arr[i].getName(),arr[i].getChinese() + arr[i].getMath() + arr[i].getEnglish()));
        }*/

       /* list.add(new Score(arr[0].getName(),
                arr[0].getChinese() + arr[0].getMath() + arr[0].getEnglish()));
        list.add(new Score(arr[1].getName(),
                arr[1].getChinese() + arr[1].getMath() + arr[1].getEnglish()));
        list.add(new Score(arr[2].getName(),
                arr[2].getChinese() + arr[2].getMath() + arr[2].getEnglish()));
        list.add(new Score(arr[3].getName(),
                arr[3].getChinese() + arr[3].getMath() + arr[3].getEnglish()));
        list.add(new Score(arr[4].getName(),
                arr[4].getChinese() + arr[4].getMath() + arr[4].getEnglish()));*/

        System.out.println("输出录入的数据: ");
        for (Score score : list) {
            System.out.println(score.getName() + " " + score.getTotalScore());
        }

        while (!list.isEmpty()) {

            for (int j = 0; j < 5; j++) {
                int max = 0;
                for (int i = 0; i < list.size(); i++) {
                    if (list.get(i).getTotalScore() > list.get(0).getTotalScore()) {
                        max = i;
                    }
                }
                System.out.println(list.get(max).getName() + " " + list.get(max).getTotalScore());
                list.remove(max);
            }

        }

    }
}

/*
        arr[0] = new Student("1姓名", 50, 100, 100);
        arr[1] = new Student("2姓名", 60, 100, 100);
        arr[2] = new Student("3姓名", 70, 100, 100);
        arr[3] = new Student("4姓名", 80, 100, 100);
        arr[4] = new Student("5姓名", 90, 100, 100);
 */

Student 类

package todayHw;

public class Student {
    private String name;
    private double Chinese;
    private double Math;
    private double English;

    public Student() {
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getChinese() {
        return Chinese;
    }

    public void setChinese(double chinese) {
        Chinese = chinese;
    }

    public double getMath() {
        return Math;
    }

    public void setMath(double math) {
        Math = math;
    }

    public double getEnglish() {
        return English;
    }

    public void setEnglish(double english) {
        English = english;
    }
}

Score类

package todayHw;

public class Score {
    private String name;
    private Double totalScore;

    public Score() {
    }

    public Score(String name, Double totalScore) {
        this.name = name;
        this.totalScore = totalScore;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Double getTotalScore() {
        return totalScore;
    }

    public void setTotalScore(Double totalScore) {
        this.totalScore = totalScore;
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值