计算数组的平均值

时间:2013-03-12 00:23:49

标签: java arrays mean

这是一个代码,用于获取用户估算的成绩,找到它们的平均值,偏差,并以表格样式显示此信息。我的程序接受用户输入并很好地显示信息,但它不会正确计算平均值和偏差。运行时,它表示平均值为0.我这样做的原因与我老师教给我们的原因相同,但我找不到我的错误。

import java.util.Scanner;

public class ClassScores{
public static void main(String[] args){
String[] names = {"Bashful","Doc","Dopey","Grumpy","Happy","Sleepy","Sneezy"};
double[] grades = new double[7];
double mean=0;
double[] difference = new double[7];

getScores(grades);
average(grades, mean);
deviation(grades,mean,difference);
displayResults(names, grades, difference, mean);
} 



public static double[] getScores(double[] grades)
{
Scanner kb= new Scanner(System.in);
System.out.println("Enter grades for students in alphabetical order.");
for (int i=0;i<grades.length; i++)
{
    grades[i]=kb.nextDouble();
}
return grades;  
}

public static double average(double[] grades, double mean)
{
double total = 0;
for (double i : grades)
{
    total += i;
}
if (grades.length>0)
{
    mean = total/grades.length;
}
return mean;
}

public static double[] deviation(double[] grades, double mean, double[] difference)
{
for (int i=0; i<grades.length; i++)
{
    difference[i]=grades[i]-mean;
}
return difference;
}

public static void displayResults(String[] names, double[] grades, double[] difference, double mean)
{
System.out.println("The average score is" +mean);
System.out.println("Student Name   Grade    Mean Deviation");

for (int i=0; i<names.length; i++)
{
    System.out.printf(names[i]);
    System.out.printf("%20f", grades[i]);
    System.out.printf("%20f", difference[i]);
    System.out.println();
}

}   

}

以下是任何有兴趣的人的编辑代码。

import java.util.Scanner;

public class ClassScores{
public static void main(String[] args){
String[] names = {"Bashful","Doc","Dopey","Grumpy","Happy","Sleepy","Sneezy"};
double[] grades = new double[7];
double mean=0;
double[] difference = new double[7];

getScores(grades);
mean = average(grades);
deviation(grades,mean,difference);
displayResults(names, grades, difference, mean);
}



public static double[] getScores(double[] grades)
{
Scanner kb= new Scanner(System.in);
System.out.println("Enter grades for students in alphabetical order.");
for (int i=0;i<grades.length; i++)
{
    grades[i]=kb.nextDouble();
}
return grades;  
}

public static double average(double[] grades)
{
double total = 0;
for (double i : grades)
{
    total += i;
}
return total/(grades.length);
}

public static double[] deviation(double[] grades, double mean, double[] difference)
{
for (int i=0; i<grades.length; i++)
{
    difference[i]=grades[i]-mean;
}
return difference;
}

public static void displayResults(String[] names, double[] grades, double[] difference, double mean)
{
System.out.println("The average score is" +mean);
System.out.println("Student Name   Grade    Mean Deviation");

for (int i=0; i<names.length; i++)
{
    System.out.printf(names[i]);
    System.out.printf("%20f", grades[i]);
    System.out.printf("%20f", difference[i]);
    System.out.println();
}

}   

}

5 个答案:

答案 0 :(得分:2)

您实际上从未使用average方法的返回结果。您可能打算将其传递给displayResults,或者将其分配到mean方法中的main()

答案 1 :(得分:1)

首先,您的平均方法应该更像这样:

public static double average(double[] grades)
{
    // Error check up front.
    if (grades.length == 0) { 
        throw new InvalidArgumentException("length is 0");
    }

    // These next lines are good.
    double total = 0;
    for (double i : grades)
    {
        total += i;
    }

    // Then you can just divide and return.
    return total / (grades.length);
}

没有理由将mean参数传递给average方法。这就像将Dog传递给createDog方法一样。

但更重要的是,当您致电average时,您不会将结果存储在任何地方。你只是忽略它。

如果您来自带有指针的背景,请记住Java通过值传递原语 - 更改meandouble,方法内部将具有对方法外的值没有影响。相反,有

double mean = average(grades);

因此,mean为零的根本原因是因为您将其设置为零,并且永远不会更改它:

double mean = 0;

答案 2 :(得分:0)

您正在返回mean值,但您没有将方法调用的结果分配给average。你想要的是将方法的结果分配给变量:

mean = average(grades);

在您的average方法中,您不需要将mean作为参数,只需在本地声明并将其返回。

答案 3 :(得分:0)

average(grades, mean);副本的变量发送到average()方法。 mean内的average()变量与mean内的main()变量无关,尽管名称相同。要解决此问题,您需要执行类似

的操作
mean = average(grades, mean);

我还建议您删除mean参数并在average()内声明一个局部变量。那你就可以做到

mean = average(grades);

答案 4 :(得分:0)

您声明此方法返回值:

public static double average(double[] grades, double mean)

但在您的main方法中,您不使用返回的结果。然后你打印出来了 另一个“mean”变量的值,该变量对于该函数是全局的,并且被赋值为0。

double mean=0;
double[] difference = new double[7];

getScores(grades);
**average(grades, mean);**
deviation(grades,mean,difference);
displayResults(names, grades, difference, mean);

我想你想要:

getScores(grades);
**mean = average(grades, mean);**
deviation(grades,mean,difference);
displayResults(names, grades, difference, mean);