二维数组平均值(Java)
我需要这个方法来获取在其他方法中创建/更改的2D数组的中间列,计算每行的平均值(不包括0索引和最后一个索引),将该平均值放在该行的最后一列,然后转到下一行并重复,直到不再有行。基本上,2行3列的设置实际上有5列,如下所示二维数组平均值(Java),java,for-loop,average,multidimensional-array,Java,For Loop,Average,Multidimensional Array,我需要这个方法来获取在其他方法中创建/更改的2D数组的中间列,计算每行的平均值(不包括0索引和最后一个索引),将该平均值放在该行的最后一列,然后转到下一行并重复,直到不再有行。基本上,2行3列的设置实际上有5列,如下所示 Weight Grade1 Grade2 Grade3 Average ---------------------------------------- 1| 4.0 5.0 5.0 5.0 -999.0 2| 5.0
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
该方法需要获取分数,找到它们的平均值,然后用平均值替换-999.0,然后对存在的下一行执行相同的操作(大小由另一种方法中的用户输入定义)。这是我的方法代码,我知道我已经很接近了,但是我在某些地方犯了一些错误
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;
}
公共静态双[][]计算类别平均值(双[][]记分数组){
for(int i=0;i
我不能完全理解你的代码。如果你能评论一下,那就太好了。但在这里我会给你一个伪代码(ish),我希望这在某种程度上有所帮助。
假设:
您的数组是双精度数组
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
}
double tempSum=0;
对于(int x=0;x<行数;x++)
{
tempSum=0;
for(int y=1(因为不包括第一个索引);y<数字量-1(因为不包括最后一个;y++)
{
tempSum+=标记[x][y];
}
标记[x][length-1]=(tempSum/length-2)//设置最后一个索引的平均值,长度-2表示索引0和最后一个索引
}
然后根据需要输出
您的代码似乎有点太复杂,无法满足需要=p我无法准确理解您的代码。如果您能对其进行注释,那就太好了。但在这里,我将为您提供一个伪代码(ish),我希望这对您有所帮助。 假设: 您的数组是双精度数组
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
}
double tempSum=0;
对于(int x=0;x<行数;x++)
{
tempSum=0;
for(int y=1(因为不包括第一个索引);y<数字量-1(因为不包括最后一个;y++)
{
tempSum+=标记[x][y];
}
标记[x][length-1]=(tempSum/length-2)//设置最后一个索引的平均值,长度-2表示索引0和最后一个索引
}
然后根据需要输出
您的代码似乎有点太复杂,无法满足需要=p尝试以下方法:
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;
}
公共静态双[][]计算类别平均值(双[][]记分数组){
for(int i=0;i
试试这个:
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;
}
公共静态双[][]计算类别平均值(双[][]记分数组){
for(int i=0;i
现在的问题是什么?是否存在任何错误/异常?现在的问题是什么?是否存在任何错误/异常?