提问人:jecca 提问时间:11/12/2023 更新时间:11/12/2023 访问量:61
有人可以解释一下我的代码是什么意思吗?我正在编写一个程序,将用户输入传递到数组中,然后计算数组的平均值
Can someone explain what my code means? I'm writing a program that passes user inputs into an array, then calculates the average of the array
问:
家庭作业如下:
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 可以区分具有不同方法签名的方法。这意味着,如果类中的方法具有不同的参数列表,则它们可以具有相同的名称(在标题为“接口和继承”的课程中将讨论一些限定条件)。
在您的示例中,您实现了两个名为 的方法,一个接受 的数组,另一个接受 的数组。这举例说明了 Java 如何区分方法,不仅通过它们的名称,还通过它们的参数列表来区分它们。average()
int
double
正如您正确观察到的,在 main 方法中仅调用版本。这是因为您正在将数组传递给该方法。Java 根据传递的参数类型选择适当的方法。如果传递了一个数组,Java 将调用该方法的版本。double
double[]
average
average
int[]
int
关于代码的一些说明,供您考虑:
您可以使用变量,而不是将数组长度硬编码为 10。这使您的代码更具适应性和可重用性。
而且,我还会避免在循环中对 10 等值进行硬编码和平均计算。请改用数组的 length 属性。
要计算 ,您可以使用增强的 for 循环。sum
您正确地从这两个方法返回平均值,但未在方法中使用这些返回值。您可以将返回的平均值存储在变量中,然后打印出来。main
请考虑为用户输入添加错误处理,例如检查无效输入(非数字等)。
你知道为什么我必须在for循环之前初始化sum吗?我试图在for循环中初始化它,但这不起作用。
在 Java 中,变量必须先声明并初始化,然后才能使用。当你在循环中声明一个变量时,它的作用域(即代码中被识别和可用的部分)被限制在该循环中。
每次循环迭代时,如果在其中声明,它将被重新初始化为其起始值,这不是您在计算总和时想要的行为。sum
sum
在循环之前初始化变量时:
int sum = 0;
或double sum = 0.0;
- 该行的初始值为 0。
sum
- 的作用域是整个方法,这意味着它在循环的所有迭代中都保留其值。
sum
- 循环的每次迭代都会将 加到 ,累加总数。
sum
在循环中初始化变量时:
- 如果在循环中声明,则在每次迭代开始时,它将设置为 0(或任何其他起始值)。
sum
- 这意味着您将丢失在先前迭代中累积的总数。
// Correct way
int sum = 0;
for (int i = 0; i < 10; i++) {
sum += i; // Adds i to sum, accumulating over each iteration
}
// Incorrect way
for (int i = 0; i < 10; i++) {
int sum = 0; // Sum is reset to 0 on each iteration
sum += i; // Only the current value of i is added, previous totals are lost
}
以正确的方式,在循环开始之前初始化一次,并在迭代中保留其值。以不正确的方式,在每次迭代中重置,因此它永远不会累积所有迭代的总和。sum
sum
如果我没有使用该返回值,为什么我需要语句?
return
即使您不在当前程序中使用方法的返回值,包括 return 语句也有多种用途:
- 您的方法使用返回类型 ( 或 ) 声明,因此它必须返回该类型的值。这是履行您在方法签名中定义的协定的一部分。
int
double
- 虽然当前实现不使用返回值,但返回计算出的平均值会使方法更加通用,以供将来使用。例如,如果以后修改程序或在其他程序中使用此方法,则可能需要平均值进行进一步的计算或决策过程。
- 确保您的方法完全按照其签名所建议的方式执行操作是一个好习惯。如果一个方法应该返回一个值,那么它应该这样做。
我是否应该将方法标头中的“”(如说明中所述)更改为我的数组名称“”?
array
userInput
在方法标头中,为参数指定的名称是占位符。你可以随心所欲地命名它们,只要这些名称有意义并遵循 Java 的命名约定即可。
方法定义中的参数名称不需要与调用方法时传递给该方法的数组的名称匹配。
public static double average(double[] array) {
// The name 'array' here is just a label for whatever double array is passed to this method.
// It could be named 'data', 'values', 'input', etc.
}
方法定义中的参数名称在方法中用于引用传递给它的值。这些名称是方法的本地名称,不会影响或依赖于代码其他部分中的变量名称。
选择具有描述性的参数名称并指示其角色或它们所代表的数据类型通常很有帮助。无论您使用 、 还是任何其他名称,理想情况下,它都应该传达参数的用途或内容。
array
userInput
更改参数名称不会影响方法的功能。参数 ( 或 ) 的类型很重要。该名称只是方法中使用的标签。
int[]
double[]
在您的例子中,在方法中使用作为参数名称是完全可以的,甚至可以通过明确指示此数组应该包含用户输入来使代码更具可读性。关键是如何在方法中使用这些名称的一致性和清晰度。userInput
评论
public static double average(double[] userInput)
double[]