diff --git a/w7/ScoreCalculator.java b/w7/ScoreCalculator.java new file mode 100644 index 0000000..c5f2c70 --- /dev/null +++ b/w7/ScoreCalculator.java @@ -0,0 +1,38 @@ +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)); + } +} \ No newline at end of file diff --git a/w7/屏幕截图 2026-04-18 210235.png b/w7/屏幕截图 2026-04-18 210235.png new file mode 100644 index 0000000..b109f54 Binary files /dev/null and b/w7/屏幕截图 2026-04-18 210235.png differ diff --git a/w7/屏幕截图 2026-04-18 210248.png b/w7/屏幕截图 2026-04-18 210248.png new file mode 100644 index 0000000..610fad1 Binary files /dev/null and b/w7/屏幕截图 2026-04-18 210248.png differ