英文:
What do I need to change for this function to calculate the corrrect average?
问题
我对Java非常陌生,我正在尝试创建一个名为Average
的类,其对象通过addNumber
方法首先可以将许多整数(另一个名为Integers的类的对象)添加到其中,然后计算这些整数的平均值。
我可以看到正确的数字被添加到对象中,并且计数器也正常工作,但是我就是找不出如何计算ArrayList
的总和,以便我可以通过将其总和除以我的计数器来计算这些数字的平均值。
对于这行代码,在这里我一直收到错误消息,无法将Integer转换为int:
for (int i : variables)
这是有道理的,但到目前为止,我尝试过的任何将其转换为int的方式似乎都不起作用。实际上,我从这里复制了那一行代码(https://coderanch.com/t/673671/java/correctly-Sum-contents-ArrayList-Integer),但是对我来说,那似乎真的不起作用。除了import java.util.*;
,我还需要导入其他内容吗?
public class Average
{
// 用户提供的整数
private ArrayList<Integer> variables;
private int count = 0;
private int sum;
// 结果平均值
private int average;
/**
* 创建对象时,从Integer类中获取数字
*/
public Average()
{
variables = new ArrayList<Integer>();
}
/**
* 添加要计算平均值的变量
*/
public void addNumber(Integer newNumber)
{
variables.add(newNumber);
count ++;
}
/**
* 计算给定变量的平均值的方法
*/
public int calcAverage(ArrayList<Integer> variables)
{
for (int i : variables)
{
sum += i;
}
return sum;
average = sum / count;
}
}
英文:
I'm very new to Java, and I'm trying to create a class Average
whose object calculates the average of a number of integers (objects of another class Integers) that can be added to it via the addNumber
method first.
I can see that the correct numbers are added to the object and that the counter works correctly too, but I just can't seem to figure out how to calculate the sum of the ArrayList
so that I can then calculate the average of these numbers by dividing its sum by my counter.
For this line here, I keep getting the error message that Integer cannot be converted to int:
for (int i : variables)
Which makes sense, but so far none of the ways I tried to convert it to one seems to work. I actually copied that one line of code from here (https://coderanch.com/t/673671/java/correctly-Sum-contents-ArrayList-Integer) but for me that really doesn't seem to work. Do I need to import anything besides import java.util.*;
?
public class Average
{
// Ints given by the user
private ArrayList<Integer> variables;
private int count = 0;
private int sum;
// Resulting average
private int average;
/**
* When creating object, takes numbers from class Integer
*/
public Average()
{
variables = new ArrayList<Integer>();
}
/**
* Add variables to calculate averages from
*/
public void addNumber(Integer newNumber)
{
variables.add(newNumber);
count ++;
}
/**
* Method to calculate the average of the given variables
*/
public int calcAverage(ArrayList<Integer> variables)
{
for (int i : variables)
{
sum += i;
}
return sum;
average = sum / count;
}
}
答案1
得分: 0
代码中存在几个问题。
首先,从API的角度来看,如果calcAverage
方法是属于该类的,为什么会将ArrayList<Integer>
作为参数传入?这个方法应该要么是static
的,要么不带任何参数,直接计算成员变量的平均值。
其次,从编码的角度来看,在将值赋给平均值之前,它返回了sum
,导致average = sum / count
这一行成为无法访问的代码,从而引发编译错误。
第三,从数学的角度来看,由于sum
和count
都是int
类型,它们的除法将被执行为整数除法,在大多数情况下会得到错误的结果。
最后,Average
类没有任何访问方法来获取添加到其中的数字,也没有修改方法来修改除了添加数字以外的列表。考虑到这个API,持有一个数字列表可能是多余的。相反,你可以只保存添加的数字的sum
和count
,并在需要时返回它们的平均值:
public class Average {
private int count = 0;
private int sum = 0;
/**
* 添加要计算平均值的变量
*/
public void addNumber(int newNumber) {
sum += newNumber;
count++;
}
/**
* 计算给定变量的平均值的方法
*/
public double calcAverage() {
return ((double) sum) / count;
}
}
英文:
There are a few issues with the code you shared here.
First, from an API perspective, why would calcAverage
take an ArrayList<Inetger>
as a parameter if the class has the variables
data member? Such a method should either be static
or take no argument and calculate the average of the member.
Second, from a coding perspective, it returns the sum
before assigning the value to the average, making the line average = sum / count
unreachable code, and causing a compilation error.
Third, from a mathematical perspective, since sum
and count
are int
s, their division will be performed as integer division, giving you the wrong result in most cases.
Finally, the Average
class doesn't have any access methods to retrieve the numbers added to it, or modification methods to modify this list other than adding numbers to it. Taking this API under consideration, it may be redundant to hold a list of numbers. Instead, you could just hold the sum
and count
of the numbers being added, and return their average on demand:
public class Average {
private int count = 0;
private int sum = 0;
/**
* Add variables to calculate averages from
*/
public void addNumber(int newNumber) {
sum += newNumber;
count++;
}
/**
* Method to calculate the average of the given variables
*/
public double calcAverage() {
return ((double) sum) / count;
}
}
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论