【发布时间】:2013-12-23 04:56:34
【问题描述】:
我对 java 很陌生,我有一个项目来制作一个程序,该程序读取一个包含数字列表的文件并计算它们。我想在读取文件后创建一个数组,但似乎无法使用我在 makeArray 方法中创建的数组'scores[]'。有人可以告诉我如何解决这个错误吗?我相信这与范围有关,我只是无法弄清楚。另外,我知道列表可以代替数组工作,但我不能在这个项目中使用。谢谢! 附言对不起,如果我的代码很丑
import java.io.*;
import java.util.*;
class GradeFiles {
public static void main(String[] args)
throws FileNotFoundException {
Scanner console = new Scanner(System.in);
Scanner input = promptUser(console);
for(int i = 0; i<= scores.length-1; i++) {
double[] printArray = scores[i];
System.out.print(printArray);
}
}
public static Scanner promptUser(Scanner console)
throws FileNotFoundException {
Scanner isThere = null;
while(isThere == null) {
System.out.print("Enter name of file: ");
String fileName = console.next();
try {
isThere = new Scanner(new File(fileName));
} catch (FileNotFoundException e) {
System.out.print("File Not Found. ");
}
}
System.out.print("");
return isThere;
}
public static double[] makeArray(Scanner input) {
int length = 0;
while(input.hasNext()) {
double a = input.nextDouble();
length++;
}
double[] scores = new double[length-1];
while(input.hasNext()) {
for(int i = 0; i <= length - 1; i++) {
double num = input.nextDouble();
scores[i] = num;
}
}
return scores;
}
}
【问题讨论】:
-
嗯,你的错误到底是什么?
标签: java arrays file scope project