[英]2D Array Average (Java)
I need this method to take the middle columns of the 2D array that was made/changed in other methods, compute the average of each row (excluding the 0 index and last index), place that average in the last column of the row, then go down to the next row and repeat until there are no more rows. 我需要此方法来获取在其他方法中进行/更改的2D数组的中间列,计算每行的平均值(不包括0索引和最后一个索引),将该平均值放在行的最后一列中,然后向下移至下一行并重复,直到没有更多行为止。 Basically a setup of 2 rows and 3 columns will actually have 5 columns as seen below.
基本上,由2行和3列组成的设置实际上将具有5列,如下所示。
Weight Grade1 Grade2 Grade3 Average
----------------------------------------
1| 4.0 5.0 5.0 5.0 -999.0
2| 5.0 10.0 10.0 10.0 -999.0
the method needs to take the Grade scores, find their average, then replace the -999.0 with the average, and then do the same for the next rows that exist(size defined by user input in another method). 该方法需要获取成绩分数,找到其平均值,然后用平均值代替-999.0,然后对存在的下一行执行相同操作(由另一种方法中的用户输入定义的大小)。 Here is my code for the method, I know I am fairly close, but I am making a few mistakes somewhere.
这是该方法的代码,我知道我已经很接近了,但是我在某个地方犯了一些错误。
public static double[][] computeCategoryAverages(double[][] scoreArray){
for(int i = 0; i < scoreArray.length; i++){
double sum = 0;
int numOfAssign = 0;
if(i == 0){
System.out.println("The Average for the first category: ");
for(int j = 1; j < scoreArray[0].length-1; j++){
sum = sum + scoreArray[0][j];
numOfAssign++;
}
double average = sum / numOfAssign;
for(int k = 0; k < scoreArray.length; k++){
//sum = sum + scoreArray[i][j];
//noOfAssign++;
scoreArray[k][scoreArray[k].length-1] = average;
}
}
else{
System.out.println("The Average for the next category: ");
for(int j = 1; j < scoreArray[0].length-1; j++){
sum = sum + scoreArray[i][j];
numOfAssign++;
}
double average = sum / numOfAssign;
for(int k = 0; k < scoreArray.length; k++){
//sum = sum + scoreArray[i][j];
//noOfAssign++;
scoreArray[k][scoreArray[k].length-1] = average;
}
}
}
return scoreArray;
}
I can't exactly understand your code. 我无法完全理解您的代码。 It'd be great if you could comment it.
如果您可以发表评论,那将是很棒的。 But there I'll give you a pseudo code (ish) and I hope this helps in some way or another.
但是在这里,我将为您提供一个伪代码(ish),希望这对您有所帮助。 Assuming: Your array is a double
假设:您的数组是一个double
double tempSum=0;
for (int x = 0; x < amount of rows; x++)
{
tempSum=0;
for(int y = 1(because excluding first index); y < amount of numbers - 1 (because excluding last; y++)
{
tempSum+= marks[x][y];
}
marks[x][length-1]=(tempSum/length-2) //setting the average to the last index, length -2 to account for index 0 and last one
}
then output as needed 然后根据需要输出
your code seems a little too complicated for what is needed =p 您的代码对于所需的内容似乎有点复杂= p
Try this: 尝试这个:
public static double[][] computeCategoryAverages(double[][] scoreArray){
for(int i = 0; i < scoreArray.length; i++){
double sum = 0;
int numOfAssign = 0;
double average=0.0;
System.out.printf("The Average for category:%d:",i+1);
for(int j = 1; j < scoreArray[i].length-1; j++){
numOfAssign++;
sum = sum + scoreArray[i][j];
}
double average = sum / numOfAssign;
scoreArray[i][scoreArray[i].length-1] = average;
}
return scoreArray;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.