gpt4 book ai didi

Java - 如何找到在 Student 类中编写方法的得分最高的学生?

转载 作者:行者123 更新时间:2023-12-02 06:11:45 27 4
gpt4 key购买 nike

我是 Java 初学者,陷入了一项任务。如果有人能帮助我,我将非常感激。我有一个具有这些属性的学生类(class) - 姓名、化学分数、数学分数和物理分数。在主课中,我与几个学生创建了一个 ArrayList。我的任务是找到所有学生中化学分数最高、数学分数最高和物理分数最高的学生(他们的名字)。我在 Main 类的 main 方法中执行了此操作,但问题是我必须在 Student 类中编写此操作,但此时我无法执行此操作。

这是我的代码:

public class Student {

protected String name;
private int chemistry;
private int mathematics;
private int physics;


public Student(String name, int chemistry, int mathematics, int physics) {
this.name = name;
this.chemistry = chemistry;
this.mathematics = mathematics;
this.physics = physics;
}

public int getChemistry() {
return chemistry;
}

public void setChemistry(int chemistry) {
this.chemistry = chemistry;
}

public int getMathematics() {
return mathematics;
}

public void setMathematics(int mathematics) {
this.mathematics = mathematics;
}

public int getPhysics() {
return physics;
}

public void setPhysics(int physics) {
this.physics = physics;
}
}
import java.util.ArrayList;

public class Main {

public static void main(String[] args) {


Student tom = new Student("Tom", 7, 6, 4);
Student rome = new Student("Rome", 9, 5, 8);
Student jack = new Student("Jack", 6, 9, 8);
Student simon = new Student("Simon", 10, 8, 5);
Student darek = new Student("Darek", 10, 9, 8);

ArrayList<Student> students = new ArrayList<>();

students.add(tom);
students.add(rome);
students.add(jack);
students.add(simon);
students.add(darek);


System.out.println("Student(s) with the highest Chemistry grade among all students:");

int max = 0;
String names = null;
for (int i = 0; i < students.size(); i++) {
if (max == students.get(i).getChemistry()) {
names += ", " + students.get(i).name;
} else if (max < students.get(i).getChemistry()) {
max = students.get(i).getChemistry();
names = students.get(i).name;
}
}
System.out.println(names);


System.out.println();
System.out.println("Student(s) with the highest Mathematics grade among all students:");

max = 0;
names = null;
for (int i = 0; i < students.size(); i++) {
if (max == students.get(i).getMathematics()) {
names += ", " + students.get(i).name;
} else if (max < students.get(i).getMathematics()) {
max = students.get(i).getMathematics();
names = students.get(i).name;
}
}
System.out.println(names);


System.out.println();
System.out.println("Student(s) with the highest Physics grade among all students:");


max = 0;
names = null;
for (int i = 0; i < students.size(); i++) {
if (max == students.get(i).getPhysics()) {
names += ", " + students.get(i).name;
} else if (max < students.get(i).getPhysics()) {
max = students.get(i).getPhysics();
names = students.get(i).name;
}
}
System.out.println(names);

}
}

我尝试在 Student 类中编写类似的方法:

public int bestOfChemistry(int max, String names) {
if (max == chemistry) {
names += ", " + name;
} else if (max < chemistry) {
max = chemistry;
names = name;
}
return max;
}

但是当我尝试在主类中使用此方法时,我只能获得最高等级。我知道,因为我只在 bestOfChemistry(..) 方法中返回了它,但我也不知道如何获取它们的名字:

int max = 0;
String names = null;
for (int i = 0; i < students.size(); i++) {
max = students.get(i).bestOfChemistry(max, names);
}
System.out.println(max);

我也不知道如何为这三个类(class)编写一个方法来避免多个相同的方法。

最佳答案

您不应该创建引用代表单个对象的类中的多个对象的方法,这在语义上没有意义。

编辑:由于您不允许在主类(class)中这样做,因此您可以添加一个新类(class),其功能是寻找最好的学生。例如,我将其命名为“StudentsFinder”。它看起来像这样:

class StudentsFinder {

private final ArrayList<Student> students;
private ArrayList<String> bestStudents;

StudentsFinder(ArrayList<Student> students) {
this.students = students;
}

ArrayList<String> getBestChemistryStudents() {
bestStudents = new ArrayList<>();
int maxChemistryGrade = 0;
//We look for the best grade first.
for (Student student : students) {
if (student.getChemistry() > maxChemistryGrade) {
maxChemistryGrade = student.getChemistry();
}
}
//Now we can add those students with the best grade in the array
for (Student student : students) {
if (student.getChemistry() == maxChemistryGrade) {
bestStudents.add(student.getName());
}
}
//And we return the results
return bestStudents;
}
//The following methods do the same thing but with math and physics grades, respectively
ArrayList<String> getBestMathStudents() {
bestStudents = new ArrayList<>();
int maxMathGrade = 0;
for (Student student : students) {
if (student.getMathematics() > maxMathGrade) {
maxMathGrade = student.getMathematics();
}
}
for (Student student : students) {
if (student.getMathematics() == maxMathGrade) {
bestStudents.add(student.getName());
}
}
return bestStudents;
}

ArrayList<String> getBestPhysicsStudents() {
bestStudents = new ArrayList<>();
int maxPhysicsGrade = 0;
for (Student student : students) {
if (student.getPhysics() > maxPhysicsGrade) {
maxPhysicsGrade = student.getPhysics();
}
}
for (Student student : students) {
if (student.getPhysics() == maxPhysicsGrade) {
bestStudents.add(student.getName());
}
}
return bestStudents;
}
}

在 Student 类中,您将需要一个名称的 setter/getter :

public String getName() {
return name;
}

然后,您可以在主类中添加一个新的 StudentsFinder 实例,在构造函数中将 Students 数组传递给它,然后调用每个方法:

StudentsFinder finder = new StudentsFinder(students);

System.out.println("Student(s) with the highest Chemistry grade among all students:");
System.out.println(finder.getBestChemistryStudents());

System.out.println("Student(s) with the highest Mathematics grade among all students:");
System.out.println(finder.getBestMathStudents());

System.out.println("Student(s) with the highest Physics grade among all students:");
System.out.println(finder.getBestPhysicsStudents());

请注意,我们可以将结果直接传递给 println() 方法:

System.out.println(finder.getBestChemistryStudents());

...因为它自动调用ArrayList中的toString(),其实现继承自AbstractCollection类。它以 [value1, value2, ..., valueN] 格式打印所有值。

关于Java - 如何找到在 Student 类中编写方法的得分最高的学生?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59239643/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com