怎么用Java写 :一个班级包含一些学生,每个学生有固定的学号与姓名,有JAVA,计算机网络,英语三门课的成绩。试采用面向对象程序设计方法,实现如下功能:
分别找出班上总成绩及单科成绩最高的学生(若有多个,应都找出来),并输出这些学生的信息;分别找出班上至少有一门课成绩低于60分、至少有两门课成绩低于60及三门课成绩均低于60分的学生,并输出这些学生的信息。允许通过输入一个学生的学号或姓名(姓名可能重复,因此可能找到多个学生)寻找学生,并输出其信息(或找到多个,则都输出)。
该回答引用chatgpt:有问题多交流
import java.util.ArrayList;
import java.util.List;
public class Student {
private String name;
private int id;
private int javaScore;
private int computerNetworkScore;
private int englishScore;
public Student(String name, int id, int javaScore, int computerNetworkScore, int englishScore) {
this.name = name;
this.id = id;
this.javaScore = javaScore;
this.computerNetworkScore = computerNetworkScore;
this.englishScore = englishScore;
}
public String getName() {
return name;
}
public int getId() {
return id;
}
public int getJavaScore() {
return javaScore;
}
public int getComputerNetworkScore() {
return computerNetworkScore;
}
public int getEnglishScore() {
return englishScore;
}
public int getTotalScore() {
return javaScore + computerNetworkScore + englishScore;
}
public boolean isScoreLow() {
return javaScore < 60 || computerNetworkScore < 60 || englishScore < 60;
}
public boolean isTwoScoreLow() {
int lowCount = 0;
if (javaScore < 60) {
lowCount++;
}
if (computerNetworkScore < 60) {
lowCount++;
}
if (englishScore < 60) {
lowCount++;
}
return lowCount >= 2;
}
public boolean isAllScoreLow() {
return javaScore < 60 && computerNetworkScore < 60 && englishScore < 60;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", id=" + id +
", javaScore=" + javaScore +
", computerNetworkScore=" + computerNetworkScore +
", englishScore=" + englishScore +
'}';
}
public static void main(String[] args) {
List<Student> studentList = new ArrayList<>();
studentList.add(new Student("Alice", 1001, 80, 75, 90));
studentList.add(new Student("Bob", 1002, 90, 85, 80));
studentList.add(new Student("Charlie", 1003, 70, 60, 65));
studentList.add(new Student("David", 1004, 85, 65, 55));
studentList.add(new Student("Eve", 1005, 60, 75, 70));
List<Student> totalScoreMaxStudents = new ArrayList<>();
int totalScoreMax = 0;
for (Student student : studentList) {
int totalScore = student.getTotalScore();
if (totalScore > totalScoreMax) {
totalScoreMaxStudents.clear();
totalScoreMaxStudents.add(student);
totalScoreMax = totalScore;
} else if (totalScore == totalScoreMax) {
totalScoreMaxStudents.add(student);
}
}
System.out.println("Total score max students: ");
for (Student student : totalScoreMaxStudents) {
System.out.println(student);
}
List<Student> javaScoreMaxStudents = new ArrayList<>();
int javaScoreMax = 0;
for (Student student : studentList) {
int javaScore = student.getJavaScore();
if (javaScore > javaScoreMax) {
javaScoreMaxStudents.clear();
javaScoreMaxStudents.add(student);
javaScoreMax = javaScore;
} else if (javaScore == javaScoreMax) {
javaScoreMaxStudents.add(student);
}
}
System.out.println("Java score max students: ");
for (Student student : javaScoreMaxStudents) {
System.out.println(student);
}
List<Student> computerNetworkScoreMaxStudents = new ArrayList<>();
int computerNetworkScoreMax = 0;
for (Student student : studentList) {
int computerNetworkScore = student.getComputerNetworkScore();
if (computerNetworkScore > computerNetworkScoreMax) {
computerNetworkScoreMaxStudents.clear();
computerNetworkScoreMaxStudents.add(student);
computerNetworkScoreMax = computerNetworkScore;
} else if (computerNetworkScore == computerNetworkScoreMax) {
computerNetworkScoreMaxStudents.add(student);
}
}
System.out.println("Computer network score max students: ");
for (Student student : computerNetworkScoreMaxStudents) {
System.out.println(student);
}
List<Student> englishScoreMaxStudents = new ArrayList<>();
int englishScoreMax = 0;
for (Student student : studentList) {
int englishScore = student.getEnglishScore();
if (englishScore > englishScoreMax) {
englishScoreMaxStudents.clear();
englishScoreMaxStudents.add(student);
englishScoreMax = englishScore;
} else if (englishScore == englishScoreMax) {
englishScoreMaxStudents.add(student);
}
}
System.out.println("English score max students: ");
for (Student student : englishScoreMaxStudents) {
System.out.println(student);
}
List<Student> scoreLowStudents = new ArrayList<>();
for (Student student : studentList) {
if (student.isScoreLow()) {
scoreLowStudents.add(student);
}
}
System.out.println("Score low students: ");
for (Student student : scoreLowStudents) {
System.out.println(student);
}
List<Student> twoScoreLowStudents = new ArrayList<>();
for (Student student : studentList) {
if (student.isTwoScoreLow()) {
twoScoreLowStudents.add(student);
}
}
System.out.println("Two score low students: ");
for (Student student : twoScoreLowStudents) {
System.out.println(student);
}
List<Student> allScoreLowStudents = new ArrayList<>();
for (Student student : studentList) {
if (student.isAllScoreLow()) {
allScoreLowStudents.add(student);
}
}
System.out.println("All score low students: ");
for (Student student : allScoreLowStudents) {
System.out.println(student);
}
String name = "Alice";
List<Student> studentsByName = new ArrayList<>();
for (Student student : studentList) {
if (student.getName().equals(name)) {
studentsByName.add(student);
}
}
System.out.println("Students by name: " + name);
for (Student student : studentsByName) {
System.out.println(student);
}
int id = 1002;
List<Student> studentsById = new ArrayList<>();
for (Student student : studentList) {
if (student.getId() == id) {
studentsById.add(student);
}
}
System.out.println("Students by ID: " + id);
for (Student student : studentsById) {
System.out.println(student);
}
}
}
上述代码中,Student
类表示一个学生,包含学号、姓名和三门课的成绩。main
方法中创建了一个学生列表,然后分别找出班上总成绩及单科成绩最高的学