简体   繁体   English

Java:查找数组中双打的平均值

[英]Java: Finding average of doubles in array

I'm working on an assignment for my intro to java class. 我正在为Java类简介做作业。 Part of the assignment is reading in doubles from a text file into an array and then using a different method to calculate the average. 作业的一部分是将文本文件中的双精度读入数组,然后使用其他方法来计算平均值。 So far I've read the doubles into the array but I'm not sure how to reference that array in another method to calculate the average. 到目前为止,我已经将双精度数读入了数组,但是我不确定如何在另一种方法中引用该数组来计算平均值。 I've been searching online, but with no results probably because my poor terminology leads me to irrelevant answers. 我一直在网上搜索,但未找到任何结果,可能是因为我的术语不正确导致我没有相关的答案。 I get an error from netbeans that the symbol classGrades can't be found(because its only in the main method.) I tried defininig it as a global variable, but that didn't work and we haven't learned about those yet so there must be another way to do it. 我从netbeans中收到一个错误,指出找不到符号classGrades(因为它仅在main方法中)。我尝试将它定义为全局变量,但这没有用,而且我们还没有学到这些必须有另一种方式来做到这一点。

To summarize: I don't know how to reference variables between methods. 总结一下:我不知道如何在方法之间引用变量。

Here is my code 这是我的代码

import java.io.File;
import java.util.Arrays;
import java.util.Scanner;

public class GradeChecker {

public static void main(String[] args) {
    // TODO code application logic here
    double[] classGrades = new double[76];
    double classAverage = calculateAverageGrade();


    fillArray(classGrades);
    for (int i = 0; i < classGrades.length; i++) {
        System.out.println(classGrades[i]);
    }
    //System.out.print(Arrays.toString(classGrades));
    System.out.print(classGrades.length);

}
    public static void fillArray(double[] ary) {
    try {
        File arrayInput = new File("ClassGrades.txt");
        Scanner in = new Scanner(arrayInput);
        in.useDelimiter("\r\n");

        int i = 0;
        while (in.hasNextLine()) {
            ary[i++] = in.nextDouble();
        }
        in.close();
    } catch (Exception exception) {
        System.out.println("Error: " + exception.getMessage());
    }
}

    private static double calculateAverageGrade(double[] classGrades) {
    double gradeSum = 0;
    double gradeAverage = 0;
    for (int i = 0.0; i < classGrades.length; i++) {
        gradeSum = gradeSum + classGrades[i];
    }
    gradeAverage = gradeSum/classGrades.length;
    return gradeAverage;
}
}

You can pass array classGrades to the function: 您可以将数组classGrades传递给该函数:

    double[] classGrades = new double[76];
    double classAverage = calculateAverageGrade(classGrades);

Here: 这里:

import java.io.File;
import java.util.Arrays;
import java.util.Scanner;

public class GradeChecker {

    public static void main(String[] args) {
        // TODO code application logic here
        double[] classGrades = new double[76];
        double classAverage = calculateAverageGrade(classGrades);


        fillArray(classGrades);
        for (int i = 0; i < classGrades.length; i++) {
            System.out.println(classGrades[i]);
        }
        //System.out.print(Arrays.toString(classGrades));
        System.out.print(classGrades.length);

    }

    public static void fillArray(double[] ary) {
        try {
            File arrayInput = new File("ClassGrades.txt");
            Scanner in = new Scanner(arrayInput);
            in.useDelimiter("\r\n");

            int i = 0;
            while (in.hasNextLine()) {
                ary[i++] = in.nextDouble();
            }
            in.close();
        } catch (Exception exception) {
            System.out.println("Error: " + exception.getMessage());
        }
    }

    private static double calculateAverageGrade(double[] classGrades) {
        double gradeSum = 0;
        double gradeAverage = 0;
        for (int i = 0; i < classGrades.length; i++) {
            gradeSum = gradeSum + classGrades[i];
        }
        gradeAverage = gradeSum / classGrades.length;
        return gradeAverage;
    }
}

I have been edited your code. 我已经编辑了您的代码。

import java.io.File;
import java.util.Arrays;
import java.util.Scanner;

public class GradeChecker {

    public static void main(String[] args) {
        // TODO code application logic here
        double[] classGrades = new double[76];
        fillArray(classGrades);

        double classAverage = calculateAverageGrade(classGrades);

        for (int i = 0; i < classGrades.length; i++) {
            System.out.println(classGrades[i]);
        }
        //System.out.print(Arrays.toString(classGrades));
        System.out.print(classGrades.length);

    }
    public static void fillArray(double[] ary) {
        try {
            File arrayInput = new File("ClassGrades.txt");
            Scanner in = new Scanner(arrayInput);
            in.useDelimiter("\r\n");

            int i = 0;
            while (in.hasNextLine()) {
                ary[i++] = in.nextDouble();
            }
            in.close();
        } catch (Exception exception) {
            System.out.println("Error: " + exception.getMessage());
        }
    }

    private static double calculateAverageGrade(double[] classGrades) {
        double gradeSum = 0;
        double gradeAverage = 0;
        for (int i = 0; i < classGrades.length; i++) {
            gradeSum = gradeSum + classGrades[i];
        }
        gradeAverage = gradeSum/classGrades.length;
        return gradeAverage;
    }
}

Please try this and let me know 请尝试这个,让我知道

EDIT 1 编辑1

  • Lot of compilation issue. 很多编译问题。
  • fillArray called after finding calculateAverageGrade 找到fillArray之后调用fillArray

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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