题目描述:
定义一个学生类用来存储学生的信息(包括:学号,姓名,英语成绩、高数成绩、体育成绩),在main方法中定义一个数组存储5名学生的信息。要求:
(1)按名字查询并输出某位同学成绩,要求能够实现部分匹配的查找
例如:希望查找John,可查找到所有名字包含John的人,
例如:John Brown,John Smith都能得到(可以使用字符串类String的contains方法,来判断某个字符串中是否包含某个子串)。
(2)查询并输出所有科目不及格的人数及名单。
输出格式为:
英语成绩不及格的有:张三、李四、王五,总共有3人
高数成绩不及格的有:张三,总共有1人
体育成绩不及格的有:张三丰、张无忌,总共有2人
提示:
(1)定义的学生类:包括属性、属性的getter、setter方法。
(2)类中其他方法可自由发挥,比如打印学生信息的方法(如print()或toString())
(3)自己定义包含main方法的类,类中定义2个方法,分别用来实现按名字查询成绩、查找不及格人数并打印不及格名单的功能。
案例代码:
public class Student {
private String studentId;
private String name;
private int englishScore;
private int mathScore;
private int sportsScore;
// 构造方法
public Student(String studentId, String name, int englishScore, int mathScore, int sportsScore) {
this.studentId = studentId;
this.name = name;
this.englishScore = englishScore;
this.mathScore = mathScore;
this.sportsScore = sportsScore;
}
// getter 和 setter 方法
public String getStudentId() {
return studentId;
}
public void setStudentId(String studentId) {
this.studentId = studentId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getEnglishScore() {
return englishScore;
}
public void setEnglishScore(int englishScore) {
this.englishScore = englishScore;
}
public int getMathScore() {
return mathScore;
}
public void setMathScore(int mathScore) {
this.mathScore = mathScore;
}
public int getSportsScore() {
return sportsScore;
}
public void setSportsScore(int sportsScore) {
this.sportsScore = sportsScore;
}
// 重写 toString 方法,用于打印学生信息
@Override
public String toString() {
return "学号:" + studentId + ",姓名:" + name + ",英语成绩:" + englishScore + ",高数成绩:" + mathScore + ",体育成绩:" + sportsScore;
}
}
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
// 创建一个存储学生信息的数组
List<Student> students = new ArrayList<>();
// 添加学生信息到数组
students.add(new Student("001", "Tom", 80, 90, 70));
students.add(new Student("002", "John Brown", 75, 85, 60));
students.add(new Student("003", "John Smith", 85, 70, 65));
students.add(new Student("004", "Mike", 60, 50, 80));
students.add(new Student("005", "Jerry", 90, 80, 75));
// 按名字查询并输出成绩
searchByName(students, "John");
// 查询不及格人数并输出名单
printFailList(students);
}
// 按名字查询并输出成绩
public static void searchByName(List<Student> students, String name) {
System.out.println("查询结果:");
for (Student student : students) {
if (student.getName().contains(name)) {
System.out.println(student);
}
}
}
// 查询不及格人数并输出名单
public static void printFailList(List<Student> students) {
int englishFailCount = 0;
int mathFailCount = 0;
int sportsFailCount = 0;
List<String> englishFailList = new ArrayList<>();
List<String> mathFailList = new ArrayList<>();
List<String> sportsFailList = new ArrayList<>();
for (Student student : students) {
if (student.getEnglishScore() < 60) {
englishFailCount++;
englishFailList.add(student.getName());
}
if (student.getMathScore() < 60) {
mathFailCount++;
mathFailList.add(student.getName());
}
if (student.getSportsScore() < 60) {
sportsFailCount++;
sportsFailList.add(student.getName());
}
}
System.out.println("英语成绩不及格的有:" + String.join("、", englishFailList) + ",总共有" + englishFailCount + "人");
System.out.println("高数成绩不及格的有:" + String.join("、", mathFailList) + ",总共有" + mathFailCount + "人");
System.out.println("体育成绩不及格的有:" + String.join("、", sportsFailList) + ",总共有" + sportsFailCount + "人");
}
}
© 版权声明
本站资源来自互联网收集,仅供用于学习和交流,请勿用于商业用途。如有侵权、不妥之处,请联系站长并出示版权证明以便删除。敬请谅解!
THE END