创建 10 个学生对象,每个学生都有姓名、性别、年龄、考试成绩属性,这些属性值可随机生成。
1.求所有学生的成绩平均值。
2.按成绩排名显示成绩单(每行显示格式:名次 姓名 成绩)。
3.假设每个学生的考试成绩由数学、语文、英语三门课组成,且成绩各不相同(可随机生成),小于 60 则挂科,若两门(或两门以上)挂科,则学生被开除。对未被开除的学生按成绩平均值进行成绩排名。
4.显示成绩单时,如果成绩相同,则按年龄由小到大排序。
package com.situ.chapter1;
public class Homework {
public static void main(String[] args) {
//创建 10 个学生对象,每个学生都有姓名、性别、年龄、考试成绩属性,这些属性值可随机生成。
//1.求所有学生的成绩平均值。
//2.按成绩排名显示成绩单(每行显示格式:名次 姓名 成绩)。
//3.假设每个学生的考试成绩由数学、语文、英语三门课组成,且成绩各不相同(可随机生成),小于 60 则挂科,
// 若两门(或两门以上)挂科,则学生被开除。对未被开除的学生按成绩平均值进行成绩排名。
//4.显示成绩单时,如果成绩相同,则按年龄由小到大排序。
Student[] students = new Student[10];
for (int i = 0; i < 10; i++) {
int age = (int) (Math.random() * 5 + 20);
Student stu = new Student("" + i, "男", age);
students[i] = stu;//两个引用指向同一个对象
int e = (int) (Math.random() * 50 + 50);//英语
int c = (int) (Math.random() * 50 + 50);//语文
int m = (int) (Math.random() * 50 + 50);//数学
stu.setEnglish(e);
stu.setChinese(c);
stu.setMath(m);
System.out.println(stu.getName() + ":"
+ stu.getAge() + ":" + stu.getChinese() + "/" + stu.getEnglish() + "/" + stu.getMath());
}
//1.
int sum = 0;
for (int i = 0; i < students.length; i++) {
sum += students[i].getTotal();
}
System.out.println(sum * 1.0 / students.length);
//2.
Clazz clazz = new Clazz(students);
clazz.printAllScoreList();
System.out.println("未被开除的学生成绩单:");
//3.
//未被开除的学生的成绩单
clazz.printAvailableScoreList();
}
}
package com.situ.chapter1;
/**
* 班级类
*/
public class Clazz {
private Student[] students;
public Clazz(Student[] students) {
this.students = students;
}
//打印指定学生的成绩单
private static void printScoreList(Student[] stus) {
for (int i = 0; i < stus.length; i++) {
System.out.println(i + 1 + "-" + stus[i].getName() + " 成绩:" + stus[i].getTotal());
}
}
/**
* 打印成绩单
*/
public void printAllScoreList() {
printS coreList(this.sortAll());
}
//打印未被开除的学生成绩单
public void printAvailableScoreList() {
printScoreList(this.sortAvailable());
}
//单纯对数组进行排序,按成绩
private static void sort(Student[] students) {
//冒泡排序
for (int i = 0; i < students.length - 1; i++) {
for (int j = 0; j < students.length - 1 - i; j++) {
if (students[j].isLessThan(students[j + 1])) {
Student t = students[j];
students[j] = students[j + 1];
students[j + 1] = t;
}
}
}
}
/**
* 对成绩进行排序
*d
* @return 返回一个排序后的数组
*/
private Student[] sortAll() {
if (this.students == null || this.students.length == 0) {
return new Student[]{};
}
Student[] stus = new Student[this.students.length];
for (int i = 0; i < this.students.length; i++) {
stus[i] = this.students[i];
}
sort(stus);
return stus;
}
/**
* 是否可用,是否顺利上课
*
* @param stu
* @return
*/
public static boolean isAvailable(Student stu) {
int cnt = 0;
if (stu.getEnglish() < 60) {
cnt++;
}
if (stu.getChinese() < 60) {
cnt++;
}
if (stu.getMath() < 60) {
cnt++;
}
return cnt < 2;
}
/**
* 对未开除学生排序
*
* @return
*/
private Student[] sortAvailable() {
if (this.students == null || this.students.length == 0) {
return new Student[]{};
}
//创建新数组,放未被开除的学生
Student[] stus = new Student[this.students.length];
int idx = 0;
for (int i = 0; i < this.students.length; i++) {
if (isAvailable(this.students[i])) {
stus[idx++] = this.students[i];
}
}
Student[] result = new Student[idx];
for (int i = 0; i < result.length; i++) {
result[i] = stus[i];
}
sort(result);
return result;
}
public Student[] getStudents() {
return students;
}
}
public class Student extends Person {
private int english;//英语成绩
private int chinese;//语文成绩
private int math;//数学成绩
public Student(String name, String sex, int age) {
super(name, sex, age);
}
public Student(String name, String sex, int age, int english, int chinese, int math) {
super(name, sex, age);
this.english = english;
this.chinese = chinese;
this.math = math;
}
public int getEnglish() {
return english;
}
public void setEnglish(int english) {
this.english = english;
}
public int getChinese() {
return chinese;
}
public void setChinese(int chinese) {
this.chinese = chinese;
}
public int getMath() {
return math;
}
public void setMath(int math) {
this.math = math;
}
/**
* 返回总成绩
*
* @return
*/
public int getTotal() {
return this.chinese + this.english + this.math;
}
//返回三门课的平均成绩
public float getAvg() {
return (int) (this.getTotal() / 3.0f * 100) / 100.0f;
}
//两个学生比较。正值0表示,负值
public int compare(Student another) {
if (this.getTotal() == another.getTotal()) {
return another.getAge() - this.getAge();
}
return this.getTotal() - another.getTotal();
}
public boolean isGreaterThan(Student another) {
return this.compare(another) > 0;
}
public boolean isLessThan(Student another) {
return this.compare(another) < 0;
}
}
package com.situ.chapter1;
public class Person {
private String name;
private String sex;
private int age;
public Person(String name, String sex, int age) {
this.name = name;
this.sex = sex;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}