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;
}
}