- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 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/
例如,我有一个父类Author: class Author { String name static hasMany = [ fiction: Book,
代码如下: dojo.query(subNav.navClass).forEach(function(node, index, arr){ if(dojo.style(node, 'd
我有一个带有 Id 和姓名的学生表和一个带有 Id 和 friend Id 的 Friends 表。我想加入这两个表并找到学生的 friend 。 例如,Ashley 的 friend 是 Saman
我通过互联网浏览,但仍未找到问题的答案。应该很容易: class Parent { String name Child child } 当我有一个 child 对象时,如何获得它的 paren
我正在尝试创建一个以 Firebase 作为我的后端的社交应用。现在我正面临如何(在哪里?)找到 friend 功能的问题。 我有每个用户的邮件地址。 我可以访问用户的电话也预订。 在传统的后端中,我
我主要想澄清以下几点: 1。有人告诉我,在 iOS 5 及以下版本中,如果您使用 Game Center 设置多人游戏,则“查找 Facebook 好友”(如与好友争夺战)的功能不是内置的,因此您需要
关于redis docker镜像ENTRYPOINT脚本 docker-entrypoint.sh : #!/bin/sh set -e # first arg is `-f` or `--some-
我是一名优秀的程序员,十分优秀!