如何在java中返回2d数组方法的平均分数?

如何在java中返回2d数组方法的平均分数?,java,arrays,loops,methods,Java,Arrays,Loops,Methods,我目前的方法工作得很好,但问题是它每次运行时都会重置平均值,因此它会一次又一次地为第一个学生显示相同的结果。 我试着使用counter++;移动到下一个学生,但仍然不起作用 public class resultsCalculator { public static void main(String[] args) { String[] classNames = {"Rick", "Tom", "Jill", "Meg

我目前的方法工作得很好,但问题是它每次运行时都会重置平均值,因此它会一次又一次地为第一个学生显示相同的结果。 我试着使用counter++;移动到下一个学生,但仍然不起作用

public class resultsCalculator {
public static void main(String[] args) {
    String[] classNames = {"Rick", "Tom", "Jill", "Megan"};
    double[][] classResults = {
            {100.0, 87.5, 95.3, 80.0},
            {95.6, 25.0, 70.7, 85.0},
            {95.3, 96.7, 82.6, 87.5},
            {61.8, 55.9, 60.1, 60.6}
    };

    System.out.println("Class results: ");
    processResults(classNames, classResults);

   public static void processResults(String[] names, double[][] scores) {
    for (int i = 0; i < names.length; i++) {
        System.out.println("Student: " + names[i]);
        System.out.print("\tAverage: ");
        for (int j = 0; j < scores[i].length; j++) {
            System.out.print(returnAverage(scores) + "\t");
        }
    }
}

public static double returnAverage (double scores[][]) {
    double average = 0;
    for (int l = 0; l < scores.length; l++) {
        double sum = 0;
        for (int j = 0; j < scores[l].length; j++) {
            sum += scores[l][j];
            average = sum / scores[l].length;
        }
    }
    return average;
}
}
公共类结果计算器{
公共静态void main(字符串[]args){
String[]classNames={“Rick”、“Tom”、“Jill”、“Megan”};
双[][]类结果={
{100.0, 87.5, 95.3, 80.0},
{95.6, 25.0, 70.7, 85.0},
{95.3, 96.7, 82.6, 87.5},
{61.8, 55.9, 60.1, 60.6}
};
System.out.println(“类结果:”);
processResults(类名称、类结果);
公共静态void processResults(字符串[]名称,双[][]分数){
for(int i=0;i
如果我理解正确,您需要整个2d数组的平均值,对吗?因此,您需要将总和除以总计数。有关其他解释,请参阅注释

public static double returnAverage (double scores[][]) {
    double sum = 0; // This should be initialized once.
    int count = 0; // This should also be initialized once, before the computation.
    for (int l = 0; l < scores.length; l++) {
        for (int j = 0; j < scores[l].length; j++) {
            sum += scores[l][j];
            count++;
        }
    }
    return sum / count; // Compute the result at the end.
}
public静态双倍返回平均值(双倍分数[]){
double sum=0;//应该初始化一次。
int count=0;//这也应该在计算之前初始化一次。
对于(int l=0;l
我假设每一行包含一个学生的分数。如果是这样,您不需要嵌套循环来计算特定学生的平均值。请参阅下面代码中的注释

public static void main(String[] args) {
        String[] classNames = {"Rick", "Tom", "Jill", "Megan"};
        double[][] classResults = {
                {100.0, 87.5, 95.3, 80.0},
                {95.6, 25.0, 70.7, 85.0},
                {95.3, 96.7, 82.6, 87.5},
                {61.8, 55.9, 60.1, 60.6}
        };

        System.out.println("Class results: ");
        processResults(classNames, classResults);
    }

    public static void processResults(String[] names, double[][] scores) {
        for (int i = 0; i < names.length; i++) {
            System.out.println("Student: " + names[i]);
            System.out.print("\tAverage: ");
            //A 2D array is an array of arrays - so here, we use the 1D array containing the scores for the current student based on the counter.
            System.out.println(returnAverage(scores[i]) + "\t");
        }
    }

    public static double returnAverage(double scores[]) {
        double sum = 0;
        
        //Add all the scores for this student - no need for a nested loop if you only process data for one student.
        for (int j = 0; j < scores.length; j++) {
            sum += scores[j];
        }

        //Calculate the average for this student.
        double average = sum/scores.length;
        return average;
    }
publicstaticvoidmain(字符串[]args){
String[]classNames={“Rick”、“Tom”、“Jill”、“Megan”};
双[][]类结果={
{100.0, 87.5, 95.3, 80.0},
{95.6, 25.0, 70.7, 85.0},
{95.3, 96.7, 82.6, 87.5},
{61.8, 55.9, 60.1, 60.6}
};
System.out.println(“类结果:”);
processResults(类名称、类结果);
}
公共静态void processResults(字符串[]名称,双[][]分数){
for(int i=0;i
2D数组中的每个条目是否都包含单个学生的分数?换句话说,分数{100.0,87.5,95.3,80.0}是否都属于学生“Rick”并且您想计算他们的平均值?您正在将整个分数[][]数组从processResults()传递到returnAverage()。您应该只传递一行,即分数[i].然后returnAverage只得到一个双[]和一个学生的分数。