Java基础知识Set练习作业—07
录入5个学生的三门成绩,按照总分从高到低排序
/*
* 录入5个学生的三门成绩,按照总分从高到低排序
* */
package day15;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class HomeWork_07 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("开始录入:");
List<Student> list = new ArrayList<Student>();
Student[] stus = new Student[5];
for(int i = 0;i<5;i++) {
String string = in.nextLine();
String[] grade = string.split(" ");
String name = grade[0];
int chn = Integer.parseInt(grade[1]);
int eng = Integer.parseInt(grade[2]);
int math = Integer.parseInt(grade[3]);
stus[i] = new Student(name,chn,eng,math);
list.add(stus[i]);
}
System.out.println("初始录入信息为:");
for(Student stu:list)
stu.print();
sort(list);
System.out.println("排序后为:");
for(Student stu:list)
stu.print();
}
private static void sort(List<Student> list) {
for(int i = 0;i<list.size()-1;i++) {
for(int j = 0;j<list.size()-1-i;j++) {
if(list.get(j).sum <list.get(j+1).sum) {
Student stemp = list.get(j);
list.set(j, list.get(j+1));
list.set(j+1,stemp);
}
}
}
}
}
class Student{
String name;
int chn;
int eng;
int math;
int sum =chn+eng+math ;
public Student(String name, int chn, int eng, int math) {
super();
this.name = name;
this.chn = chn;
this.eng = eng;
this.math = math;
sum =chn+eng+math;
}
public void print() {
System.out.println("姓名 : "+name+"\t语文:"+chn+"\t英语: "+eng+"\t数学"+math+"\t总分"+sum);
}
}