/**
*
* 一、Student类,包括学生姓名、性别、年龄、Java成绩。要求创建五个该类对象,
* 输出每个学生信息,计算并输出这五个学生Java成绩的平均值,
* 以及计算并输出他们Java成绩的最高分以及最低分是多少,分别是哪位同学。
*
*/
public class Student {
public String name;
public String gender;
public int age;
private double score;
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
public Student(String name, String gender, int age, double score) {
super();
this.name = name;
this.gender = gender;
this.age = age;
this.score = score;
}
public String toString() {
return "Student [name=" + name + ", gender=" + gender + ", age=" + age + ", score=" + score + "]";
}
public static void display(Student[] stu) {
double sum = 0;
for (int i = 0; i < stu.length; i++) {
double s = stu[i].getScore();
sum = sum + s;
}
double av = sum / stu.length;
System.out.println("JAVA平均成绩为:" + av);
}
public static void dp(Student[] stu) {
System.out.println("学生成绩汇总表:");
for (int i = 0; i < stu.length; i++) {
System.out.print(stu[i] + "\n");
}
}
public static void sort(Student[] stu) {
for (int i = 0; i < stu.length; i++) {
for (int j = i; j < stu.length; j++) {
if (stu[i].getScore() < stu[j].getScore()) {
Student tem = stu[j];
stu[j] = stu[i];
stu[i] = tem;
}
}
}
System.out.println("成绩最好的" + stu[0].toString());
System.out.println("成绩最差的" + stu[4].toString());
}
public static void main(String[] args) {
Student[] stu = new Student[5];
Student s1 = new Student("李四", "女", 20, 95.5);
Student s2 = new Student("王二", "男", 23, 98.5);
Student s3 = new Student("麻子", "女", 23, 94.0);
Student s4 = new Student("胡八一", "女", 20, 96.0);
Student s5 = new Student("张三", "男", 21, 91.0);
stu[0] = s1;
stu[1] = s2;
stu[2] = s3;
stu[3] = s4;
stu[4] = s5;
Student.display(stu);
Student.sort(stu);
Student.dp(stu);
}
}