package w7; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class ScoreAvg2 { public static void main(String[] args) { String path = "scores.txt"; int sum = 0; int count = 0; try (BufferedReader br = new BufferedReader(new FileReader(path))) { String line; while ((line = br.readLine()) != null) { try { sum += Integer.parseInt(line); count++; } catch (NumberFormatException e) { System.err.println("跳过无效数字行:" + line); } } if (count > 0) { System.out.printf("平均分:%.2f%n", (double) sum / count); } else { System.out.println("未读取到有效成绩"); } } catch (IOException e) { System.err.println("读取文件失败:" + e.getMessage()); e.printStackTrace(); } } }