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.
36 lines
1.3 KiB
36 lines
1.3 KiB
import java.io.BufferedReader;
|
|
import java.io.FileReader;
|
|
import java.io.FileNotFoundException;
|
|
import java.io.IOException;
|
|
|
|
public class Try {
|
|
public static void main(String[] args) {
|
|
int sum = 0;
|
|
int count = 0;
|
|
|
|
try (BufferedReader br = new BufferedReader(new FileReader("scores.txt"))) {
|
|
String line;
|
|
while ((line = br.readLine()) != null) {
|
|
try {
|
|
sum += Integer.parseInt(line);
|
|
count++;
|
|
} catch (NumberFormatException e) {
|
|
System.out.println("警告:第" + (count + 1) + "行不是有效的数字: " + line);
|
|
}
|
|
}
|
|
|
|
if (count > 0) {
|
|
System.out.println("总分:" + sum);
|
|
System.out.println("平均分:" + (sum * 1.0 / count));
|
|
} else {
|
|
System.out.println("没有有效的成绩数据");
|
|
}
|
|
} catch (FileNotFoundException e) {
|
|
System.out.println("错误:文件不存在或无法访问: " + e.getMessage());
|
|
} catch (IOException e) {
|
|
System.out.println("错误:文件读取失败: " + e.getMessage());
|
|
} catch (Exception e) {
|
|
System.out.println("错误:发生未知错误: " + e.getMessage());
|
|
}
|
|
}
|
|
}
|