我已经有 2 年多没有编码了,而且非常生疏,但是在谷歌上搜索了一段时间后,我无法弄清楚我做错了什么。首先我会解释这个程序,即使它很简单。我有一个 .txt 文件,其中包含 680 个数字,每行一个数字,并试图找到范围为 000-999 的数字的频率。我相信我可以弄清楚频率部分,因为它看起来很基本,但是我无法弄清楚如何从 .txt 文件中导入数字。这是我遇到的错误:
C:\Users\Arthur\Documents\FrequencyStraightPlay\FrequencyStraightPlay.java:17: error: variable sc might not have been initialized
while (sc.hasNextInt()) {
^
1 error
编码:
import java.io.*;
import java.util.*;
public class FrequencyStraightPlay {
public static void main(String[] args) {
int [] rawNumbers = new int [680];
int i = 0;
Scanner sc;
try {
sc = new Scanner(new File("Numbersnospaces.txt"));
} catch (FileNotFoundException e) {
System.out.println("File not Found!");
}
while (sc.hasNextInt()) {
rawNumbers[i++] = sc.nextInt();
}
System.out.println("The Raw Numbers: ");
for (i = 0; i < 680; i++) {
System.out.println(rawNumbers[i]);
}
}
}