0

这是一个代码,用于获取用户估算的成绩,找到他们的平均值,偏差,并以表格样式显示此信息。我的程序接受用户输入并很好地显示信息,但它不会正确计算平均值和偏差。运行时,它显示平均值为 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();
}

}   

}

这是任何好奇的人的编辑代码。

导入 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();
}

}   

}
4

5 回答 5

2

您从未真正使用过该average方法的返回结果。您可能打算将其传递给displayResults,或将其分配给mean方法中main()

于 2013-03-12T00:28:08.947 回答
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方法。这就像将 a 传递Dog给一个createDog方法。

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

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

double mean = average(grades);

所以为什么mean是零的根本原因是因为你将它设置为零,并且永远不会改变它:

double mean = 0;
于 2013-03-12T00:29:38.420 回答
0

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

mean = average(grades);

而且在你的average方法中,不需要mean作为参数,只需在本地声明并返回即可。

于 2013-03-12T00:28:42.667 回答
0

average(grades, mean);将变量的副本average()发送到方法。尽管同名,但里面的变量与mean里面average()的变量没有任何关系。要解决此问题,您需要执行类似的操作meanmain()

mean = average(grades, mean);

我还建议您删除mean参数并在average(). 然后你可以做

mean = average(grades);
于 2013-03-12T00:31:08.263 回答
0

您已声明此方法以返回一个值:

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

但是在您的 main 方法中,您不使用返回的结果。然后,您将打印出该函数全局的另一个“均值”变量的值,并被赋值为 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);
于 2013-03-12T00:31:48.607 回答