家庭作业如下:
Write two overloaded methods that return the average of an array with the following headers:
**public static int average(int\[\] array)**
**public static double average(double\[\] array)**
Write a test program that prompts the user to enter ten double values, invokes this method, and displays the average value.
我已经编写了我的代码,并且我认为它有效,但是我对作业本身的某些区域和我的代码的某些区域有点困惑。虽然我写了两个方法,但我相信只使用了 double 方法。这是正确的还是我误解了作业?
我也不太理解我的代码的某些部分。我摆弄了一些东西,直到我能够让它工作,但我不知道为什么有些部分会这样工作。
这是我的代码:
import java.util.Scanner; //Scanner is used to allow user input
class AvgArray {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double[] userInput = new double[10];// creates an array called userInput with 10 doubles in that array
System.out.printf("Please input 10 numbers: ");
for (int i = 0; i < 10; i++) {
userInput[i] = input.nextDouble();// the next input will be added to the userInput array in location i
}
average(userInput); // calls the method average and passes the userInput array to that method
input.close(); // close input to prevent resource leak
}
public static int average(int[] userInput)
// userInput is necessary at end to make it accessible for the following
// statements (Is this correct? The assignment said the header should include
// "array)", but I'm not sure whether this is something I'm supposed to fill in
// with my array name or something I should have left alone.)
{
int sum = 0;
// necessary so sum can be used in following for statement, not sure why I
// couldn't initialize below with int sum = userInput[]i + sum;
for (int i = 0; i < 10; i++) {
sum = userInput[i] + sum;
}
int average = sum / 10;
System.out.printf("%nThe average of your values is %d.%n%n", average);
return average; // necessary but not sure why
}
public static double average(double[] userInput)
// userInput is necessary at end to make it accessible for the following
// statements (Is this correct? The assignment said the header should include
// "array)", but I'm not sure whether this is something I'm supposed to fill in
// with my array name or something I should have left alone.)
{
double sum = 0;
// necessary so sum can be used in following for statement, not sure why I
// couldn't initialize below with double sum = userInput[]i + sum;
for (int i = 0; i < 10; i++) {
sum = userInput[i] + sum;
}
double average = sum / 10;
System.out.printf("%nThe average of your values is %.2f.%n%n", average);
return average; // necessary but not sure why
}
}
我相信一切正常,因为程序运行时我得到了正确的值,但我觉得我不明白我做了什么以及为什么。我问过我的教授,但他花了大约一周的时间才回复(如果他有的话)。谢谢您的帮助!
虽然我写了两个方法,但我相信只使用了 double 方法。这是正确的还是我误解了作业?
您的作业的主要挑战和目标是演示 Java 中方法重载的概念。
方法重载是一种允许多个方法具有相同名称和不同参数的功能。
Java编程语言支持重载方法,Java可以区分具有不同方法签名的方法。这意味着,如果类中的方法具有不同的参数列表,则它们可以具有相同的名称(对此有一些限定条件,将在标题为“接口和继承”的课程中讨论)。
在您的例子中,您实现了两种名为
average()
的方法,一种接受 int
数组,另一种接受 double
数组。这说明了 Java 如何不仅通过名称来区分方法,还通过参数列表来区分方法。
正如您所观察到的,在您的主方法中仅调用
double
版本。这是因为您将 double[]
数组传递给 average
方法。 Java 根据传递的参数类型选择适当的 average
方法。如果传递了 int[]
数组,Java 将调用该方法的 int
版本。
关于您的代码的一些注释,供您考虑:
您可以使用变量,而不是将数组长度硬编码为 10。这使您的代码更具适应性和可重用性。
我还会避免在循环和平均值计算中硬编码诸如 10 之类的值。请改用数组的长度属性。
为了计算
sum
,您可以使用 增强型 for 循环。
您正确地从这两种方法返回平均值,但您没有在
main
方法中使用这些返回值。您可以将返回的平均值存储在变量中,然后打印它。
考虑为用户输入添加错误处理,例如检查无效输入(非数字等)。