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.

38 lines
1.3 KiB

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class ScoreCalculator {
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) {
line = line.trim();
if (line.isEmpty()) continue;
try {
int score = Integer.parseInt(line);
sum += score;
count++;
} catch (NumberFormatException e) {
System.err.println("警告:跳过无效数字格式的行 —— " + line);
}
}
} catch (IOException e) {
System.err.println("错误:文件读取失败 —— " + e.getMessage());
return;
}
if (count == 0) {
System.err.println("错误:未找到任何有效分数,无法计算平均分。");
return;
}
double average = (double) sum / count;
System.out.println("总分:" + sum);
System.out.println("平均分:" + String.format("%.2f", average));
}
}