晚上好🌙🌙🌙
本答案参考ChatGPT-3.5
分析:
首先需要定义一个学生类,保存每一个学生的信息,包括学号、姓名、英语成绩、高数成绩、体育成绩。为了方便查询并输出所有科目不及格的学生,需要在学生类中定义查询不及格的方法,并在这个方法中判断哪些科目成绩不及格。
然后在main方法中定义一个数组,用来存储5名学生的信息,并实例化这5名学生的学生类对象。接着定义两个方法:按名字查询成绩的方法和查找不及格人数的方法,在这两个方法中遍历学生数组,调用学生类中定义的查询不及格的方法,判断哪些学生的成绩不及格。
最后将查询结果输出。
解决方案:
- 定义学生类。
public class Student {
private String id; // 学号
private String name; // 姓名
private int englishScore; // 英语成绩
private int mathScore; // 高数成绩
private int peScore; // 体育成绩
// 构造方法
public Student(String id, String name, int englishScore, int mathScore, int peScore) {
this.id = id;
this.name = name;
this.englishScore = englishScore;
this.mathScore = mathScore;
this.peScore = peScore;
}
// Getter和Setter方法
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
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 getPeScore() {
return peScore;
}
public void setPeScore(int peScore) {
this.peScore = peScore;
}
// 查询不及格的方法
public boolean isEnglishFailed() {
return englishScore < 60;
}
public boolean isMathFailed() {
return mathScore < 60;
}
public boolean isPeFailed() {
return peScore < 60;
}
// 打印学生信息的方法
@Override
public String toString() {
return "学号:" + id + ",姓名:" + name + ",英语成绩:" + englishScore +
",高数成绩:" + mathScore + ",体育成绩:" + peScore;
}
}
- 在main方法中定义一个学生数组,并实例化5个学生类对象。
public static void main(String[] args) {
Student[] students = new Student[5];
students[0] = new Student("001", "张三", 70, 80, 90);
students[1] = new Student("002", "李四", 50, 60, 70);
students[2] = new Student("003", "王五", 80, 90, 50);
students[3] = new Student("004", "John Brown", 65, 75, 85);
students[4] = new Student("005", "John Smith", 75, 85, 95);
}
- 定义按名字查询成绩的方法。
public static void searchStudentByName(String name, Student[] students) {
System.out.println("按名字查询结果如下:");
for (Student student : students) {
if (student.getName().contains(name)) {
System.out.println(student);
}
}
}
- 定义查找不及格人数的方法。
public static void searchFailedStudents(Student[] students) {
int englishFailedCount = 0;
int mathFailedCount = 0;
int peFailedCount = 0;
List<String> englishFailedList = new ArrayList<>();
List<String> mathFailedList = new ArrayList<>();
List<String> peFailedList = new ArrayList<>();
for (Student student : students) {
if (student.isEnglishFailed()) {
englishFailedCount++;
englishFailedList.add(student.getName());
}
if (student.isMathFailed()) {
mathFailedCount++;
mathFailedList.add(student.getName());
}
if (student.isPeFailed()) {
peFailedCount++;
peFailedList.add(student.getName());
}
}
// 输出不及格人数及名单
System.out.println("英语成绩不及格的有:" + String.join("、", englishFailedList) + ",总共有" + englishFailedCount + "人");
System.out.println("高数成绩不及格的有:" + String.join("、", mathFailedList) + ",总共有" + mathFailedCount + "人");
System.out.println("体育成绩不及格的有:" + String.join("、", peFailedList) + ",总共有" + peFailedCount + "人");
}
- 在main方法中分别调用按名字查询成绩和查找不及格人数的方法,输出查询结果。
public static void main(String[] args) {
// 实例化5名学生
Student[] students = new Student[5];
students[0] = new Student("001", "张三", 70, 80, 90);
students[1] = new Student("002", "李四", 50, 60, 70);
students[2] = new Student("003", "王五", 80, 90, 50);
students[3] = new Student("004", "John Brown", 65, 75, 85);
students[4] = new Student("005", "John Smith", 75, 85, 95);
// 按名字查询成绩
searchStudentByName("John", students);
// 查找不及格人数
searchFailedStudents(students);
}
完整代码:
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
// 实例化5名学生
Student[] students = new Student[5];
students[0] = new Student("001", "张三", 70, 80, 90);
students[1] = new Student("002", "李四", 50, 60, 70);
students[2] = new Student("003", "王五", 80, 90, 50);
students[3] = new Student("004", "John Brown", 65, 75, 85);
students[4] = new Student("005", "John Smith", 75, 85, 95);
// 按名字查询成绩
searchStudentByName("John", students);
// 查找不及格人数
searchFailedStudents(students);
}
// 按名字查询学生成绩
public static void searchStudentByName(String name, Student[] students) {
System.out.println("按名字查询结果如下:");
for (Student student : students) {
if (student.getName().contains(name)) {
System.out.println(student);
}
}
}
// 查找不及格人数并输出不及格名单
public static void searchFailedStudents(Student[] students) {
int englishFailedCount = 0;
int mathFailedCount = 0;
int peFailedCount = 0;
List<String> englishFailedList = new ArrayList<>();
List<String> mathFailedList = new ArrayList<>();
List<String> peFailedList = new ArrayList<>();
for (Student student : students) {
if (student.isEnglishFailed()) {
englishFailedCount++;
englishFailedList.add(student.getName());
}
if (student.isMathFailed()) {
mathFailedCount++;
mathFailedList.add(student.getName());
}
if (student.isPeFailed()) {
peFailedCount++;
peFailedList.add(student.getName());
}
}
// 输出不及格人数及名单
System.out.println("英语成绩不及格的有:" + String.join("、", englishFailedList) + ",总共有" + englishFailedCount + "人");
System.out.println("高数成绩不及格的有:" + String.join("、", mathFailedList) + ",总共有" + mathFailedCount + "人");
System.out.println("体育成绩不及格的有:" + String.join("、", peFailedList) + ",总共有" + peFailedCount + "人");
}
// 学生类
public static class Student {
private String id; // 学号
private String name; // 姓名
private int englishScore; // 英语成绩
private int mathScore; // 高数成绩
private int peScore; // 体育成绩
// 构造方法
public Student(String id, String name, int englishScore, int mathScore, int peScore) {
this.id = id;
this.name = name;
this.englishScore = englishScore;
this.mathScore = mathScore;
this.peScore = peScore;
}
// Getter和Setter方法
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
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 getPeScore() {
return peScore;
}
public void setPeScore(int peScore) {
this.peScore = peScore;
}
// 查询不及格的方法
public boolean isEnglishFailed() {
return englishScore < 60;
}
public boolean isMathFailed() {
return mathScore < 60;
}
public boolean isPeFailed() {
return peScore < 60;
}
// 打印学生信息的方法
@Override
public String toString() {
return "学号:" + id + ",姓名:" + name + ",英语成绩:" + englishScore +
",高数成绩:" + mathScore + ",体育成绩:" + peScore;
}
}
}