You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
20 lines
731 B
20 lines
731 B
import java.io.BufferedReader;
|
|
import java.io.FileReader;
|
|
|
|
public class ScoreRaw {
|
|
public static void main(String[] args) {
|
|
// 【错误点】:没有 try-catch,如果文件不存在会直接报错
|
|
// 【错误点】:没有定义 sum
|
|
int sum = 0;
|
|
int count = 0;
|
|
|
|
// 【错误点】:写法不严谨,资源未在 finally 中关闭,容易造成内存泄漏
|
|
BufferedReader br = new BufferedReader(new FileReader("scores.txt"));
|
|
String line;
|
|
|
|
while ((line = br.readLine()) != null) {
|
|
// 【错误点】:如果文件里有空行或文字,parseInt 会报错崩溃
|
|
sum += Integer.parseInt(line);
|
|
}
|
|
}
|
|
}
|