From a647170cf0ac98dc36f3930c22faa0ef82ec555e Mon Sep 17 00:00:00 2001 From: XuJingwang Date: Thu, 23 Apr 2026 20:15:12 +0800 Subject: [PATCH] =?UTF-8?q?W7-=E5=BE=90=E6=99=AF=E6=97=BA-202414010701?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- W7/ScoreAvg.java | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 W7/ScoreAvg.java diff --git a/W7/ScoreAvg.java b/W7/ScoreAvg.java new file mode 100644 index 0000000..8e7f936 --- /dev/null +++ b/W7/ScoreAvg.java @@ -0,0 +1,29 @@ +public class ScoreAvg { + public static void main(String[] args) { + int sum = 0; + int count = 0; + double average = 0; + try (java.io.BufferedReader br = new java.io.BufferedReader(new java.io.FileReader("scores.txt"))) { + String line; + while ((line = br.readLine()) != null) { + try { + int score = Integer.parseInt(line.trim()); + sum += score; + count++; + } catch (NumberFormatException e) { + System.out.println("格式错误跳过该行" + line); + } + } + if (count > 0) { + average = (double) sum / count; + System.out.println("总分:" + sum); + System.out.println("人数:" + count); + System.out.println("平均分:" + average); + } else { + System.out.println("文件内无有效数据"); + } + } catch (java.io.IOException e) { + System.out.println("文件操作出错:" + e.getMessage()); + } + } +} \ No newline at end of file