简体   繁体   中英

How to return the average of scores in 2d array method in java?

My current method works fine, but the problem is it keeps resetting the average every time it runs thus it keeps displaying the same result for the first student over and over again. I tried using counter++; to move to the next student, but still it doesn't work.

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;
}
}

If I understood correctly, you need the average of the whole 2d array, right? So, then, you need to divide the total sum to the total count. See the comments for additional explanations.

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.
}

I'm working on the assumption that each row contains scores for one student. If so, you don't need a nested loop to calculate a specific student's average. See the comments in the code below.

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;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM