我正在为我的java类简介工作。部分任务是将文本文件中的双精度数读入数组,然后使用不同的方法计算平均值。到目前为止,我已经将双打读入数组,但我不确定如何在另一种方法中引用该数组来计算平均值。我一直在网上搜索,但没有结果可能是因为我糟糕的术语导致我无关紧要的答案。我从netbeans那里得到一个错误,即符号classGrades无法找到(因为它仅在主方法中。)我尝试将它定义为一个全局变量,但这并没有起作用,我们还没有找到它。我已经了解了这些,所以必须有另一种方法来做到这一点。
总结一下:我不知道如何在方法之间引用变量。
这是我的代码
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;
}
}
答案 0 :(得分:1)
您可以将数组classGrades
传递给函数:
double[] classGrades = new double[76];
double classAverage = calculateAverageGrade(classGrades);
下面:
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;
}
}
答案 1 :(得分:0)
我已经编辑了你的代码。
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;
}
}
请尝试这个并告诉我
编辑1
fillArray