英文:
Calculating a minimum in object arrays and then using it in another calculation
问题
我试图理解如何处理一个包含多个对象的对象数组,这些对象具有姓名和4个数字,然后找到最低的测试分数。我有一个名为 Student 的对象,它有一个姓名、奖励积分、第一次测试成绩、第二次测试成绩和第三次测试成绩。我需要确保这三个测试成绩相加不超过 300 分。
然后,我需要找到每个学生所输入的3个成绩中的最低测试成绩,然后能够将奖励积分添加到这些成绩中,并打印出新的成绩。
我打算创建一个方法来循环遍历 Student 对象数组,并找到每个学生的最低分,然后创建一个仅包含最低分的新数组,并将其添加到每个学生的奖励积分中,但我实际上不太清楚如何完成这部分。这是我目前创建的 Student 对象的代码:
public class Student
{
private String name;
private int bonusPoints;
private int test1;
private int test2;
private int test3;
public static final int MAX_TOTAL = 300;
public Student(String n, int bP, int g1, int g2, int g3)
{
setName(n);
setBonus(bP);
setTest1(g1);
setTest2(g2);
setTest3(g3);
}
//Setters & getters
//检查所有 3 个测试成绩之和的方法
public boolean checkTotal(Student[] array)
{
int total = 0;
for (int i = 0; i < array.length; i++)
{
total += //我不太清楚如何引用这里的 test1、test2 和 test3
}
if (total > MAX_TOTAL)
{
return false;
}
else
{
return true;
}
}
}
英文:
I'm trying to understand how to take an object array that contains objects that have a name and 4 numbers and finding the lowest test grade. I have this Student object that has a name, bonus points earned, test 1 grade, test 2 grade, and a test 3 grade. I need to make sure that all 3 test grades added together don't go over 300 points.
I then need to find the lowest test grade of the 3 entered grades for each student and then be able to add the bonus points to them and print out the new grade.
I was going to create a method to loop through the Student object array and find the lowest grade for each student, have it create a new array with just the lowest grades and add that to the bonus points earned for each student but I'm not actually sure how to go about doing that part. This is the code for the Student object I've created so far:
public class Student
{
private String name;
private int bonusPoints;
private int test1;
private int test2;
private int test3;
public static final int MAX_TOTAL = 300;
public Student(String n, int bP, int g1, int g2, int g3)
{
setName(n);
setBonus(bP);
setTest1(g1);
setTest2(g2);
setTest3(g3);
}
//Setters & getters
//Method to check total of all 3 test grades
public boolean checkTotal(Student[] array)
{
int total = 0;
for (int i = 0; i < array.length; i++)
{
total += //I'm not sure how to refer to test1 ,test2, and test3 here
}
if (total > MAX_TOTAL)
{
return false;
}
else
{
return true;
}
}
}
答案1
得分: 0
为了将这三个测试总分相加并检查是否超过最大值,我会这样做:
public boolean checkTotal()
{
int total = this.test1 + this.test2 + this.test3;
return total > MAX_TOTAL;
}
在类内部引用类的变量时,你可以使用关键字“this”来引用该类的当前实例。因此,对该对象的方法调用可能会像这样:
Student studentOne = new Student("Foo", 90, 80, 70, 10);
if (!studentOne.checkTotal())
// 进行某些操作
else
// 进行其他操作
此外,在返回布尔值时,大多数情况下不需要使用if/else。你可以简单地写成:
return /*你的布尔表达式*/;
英文:
So in order to add the 3 test totals together and check if it exceeds a maximum value. I would do something like this.
public boolean checkTotal()
{
int total = this.test1 + this.test2 + this.test3;
return total > MAX_TOTAL;
}
When referring to a classes variables within that class you can use the 'this' keyword to refer to the current instance of that class.
So a method call of that object would look something like this:
Student studentOne = new Student("Foo",90,80,70,10);
if(!studentOne.checkTotal())
//do something
else
//do something else
Also when returning a boolean you don't need an if/else most of the time. You can simply put
return /*your boolean expression here*/;
专注分享java语言的经验与见解,让所有开发者获益!
评论