创建平均数组
Creating an average array
我正在处理的作业的一部分是让我通过一种方法传递一个数组,该方法一次计算最后一个数组中 5 个数字的元素的平均值。
例如,假设 Array1 包含 {1, 2, 3, 4, 5, 6}
该方法将计算 {1, 2, 3, 4, 5} 的平均值,然后计算 {2, 3, 4, 5, 6}
然后该方法会取这些平均值并将它们放入一个新数组中,然后将该数组传回主数组。
我只是不确定从哪里开始。我在逻辑上能想到的最多的是我将需要使用嵌套循环。
是的,这是我编程的第一年。
欢迎来到 Stack Overflow,托尼!在 Stack Overflow,我们真的鼓励用户提供一些努力或研究的证明,在以后的帖子中记住这一点:)
让我们从逻辑上考虑这个问题。
我们想从 array[0]
到 array[n-2]
的数组的平均值开始(你使用 n-2,因为索引 n-1 是实际上持有值“6”)。
第二部分。从array[1]
开始,然后去array[n-1]
一旦我们知道了这一点,我们就可以取平均值并return它。
有这里不需要嵌套循环,在编程时记住这个概念,可以省去很多眼泪:保持简单
这是一个已发布的类似问题:How to minpulate arrays and find the average
这是我想出的解决方案。当您处于程序的设计阶段时,您想考虑如何使您的代码可重用。有时您会有一个复杂的程序,许多部分需要对不同的数据执行相同的操作。这就是所谓的代码可重用性,掌握它会让您的生活更轻松。
public static void main(String[] args) {
int [] arr = new int [] {1, 2, 3, 4, 5, 6}; //Stores the numbers we need to average
//We get the Lower-Average by starting at index 0, going to index n-2
System.out.println ("Lower-Average: " + average(0, arr.length - 2, arr));
//We get the Upper-Average by starting at index 1, going to index n-1
System.out.println ("Upper-Average: " + average(1, arr.length - 1, arr));
}
/*
* This method accepts a start index, end index, and an array to operate on
* The average is calculated iteratively and returned based on number of elements provided
*/
public static double average (int startIndex, int endIndex, int [] array) {
double avg = 0; //Stores the average
int counter; //Used to hold number of elements iterated through
for (counter = startIndex; counter <= endIndex; counter++) {
avg += array[counter]; //Summation for the average
}
return avg = avg / counter; //Calculate the average and return it to caller
}
输出:
Lower-Average: 3.0
Upper-Average: 3.3333333333333335
我正在处理的作业的一部分是让我通过一种方法传递一个数组,该方法一次计算最后一个数组中 5 个数字的元素的平均值。
例如,假设 Array1 包含 {1, 2, 3, 4, 5, 6} 该方法将计算 {1, 2, 3, 4, 5} 的平均值,然后计算 {2, 3, 4, 5, 6}
然后该方法会取这些平均值并将它们放入一个新数组中,然后将该数组传回主数组。
我只是不确定从哪里开始。我在逻辑上能想到的最多的是我将需要使用嵌套循环。
是的,这是我编程的第一年。
欢迎来到 Stack Overflow,托尼!在 Stack Overflow,我们真的鼓励用户提供一些努力或研究的证明,在以后的帖子中记住这一点:)
让我们从逻辑上考虑这个问题。
我们想从 array[0]
到 array[n-2]
的数组的平均值开始(你使用 n-2,因为索引 n-1 是实际上持有值“6”)。
第二部分。从array[1]
开始,然后去array[n-1]
一旦我们知道了这一点,我们就可以取平均值并return它。
有这里不需要嵌套循环,在编程时记住这个概念,可以省去很多眼泪:保持简单
这是一个已发布的类似问题:How to minpulate arrays and find the average
这是我想出的解决方案。当您处于程序的设计阶段时,您想考虑如何使您的代码可重用。有时您会有一个复杂的程序,许多部分需要对不同的数据执行相同的操作。这就是所谓的代码可重用性,掌握它会让您的生活更轻松。
public static void main(String[] args) {
int [] arr = new int [] {1, 2, 3, 4, 5, 6}; //Stores the numbers we need to average
//We get the Lower-Average by starting at index 0, going to index n-2
System.out.println ("Lower-Average: " + average(0, arr.length - 2, arr));
//We get the Upper-Average by starting at index 1, going to index n-1
System.out.println ("Upper-Average: " + average(1, arr.length - 1, arr));
}
/*
* This method accepts a start index, end index, and an array to operate on
* The average is calculated iteratively and returned based on number of elements provided
*/
public static double average (int startIndex, int endIndex, int [] array) {
double avg = 0; //Stores the average
int counter; //Used to hold number of elements iterated through
for (counter = startIndex; counter <= endIndex; counter++) {
avg += array[counter]; //Summation for the average
}
return avg = avg / counter; //Calculate the average and return it to caller
}
输出:
Lower-Average: 3.0
Upper-Average: 3.3333333333333335