题目要求:
定义一个学生类;
包括学生学号 姓名 语数外成绩
要求输入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();
}
}
}