From c574d7357ee12620f8efc6acc62e05e5cbc0fa64 Mon Sep 17 00:00:00 2001 From: zhangsiyuan <3837703520@qq.com> Date: Thu, 16 Apr 2026 20:18:04 +0800 Subject: [PATCH] =?UTF-8?q?w7-=E5=BC=A0=E6=80=9D=E6=B8=8A-202401070104?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- w7/Scorecalculator.java | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 w7/Scorecalculator.java diff --git a/w7/Scorecalculator.java b/w7/Scorecalculator.java new file mode 100644 index 0000000..84151f0 --- /dev/null +++ b/w7/Scorecalculator.java @@ -0,0 +1,29 @@ +import java.io.*; +public class Scorecalculator{ + public static void main(String[] args) { + String filepath="scores.txt"; + int sum=0; + int count=0; + try (BufferedReader br = new BufferedReader(new FileReader(filepath))){ + String line; + while ((line=br.readLine()) != null) { + try { + sum+=Integer.parseInt(line.trim()); + count++; + } catch (NumberFormatException e){ + System.out.println("数字格式错误,跳过该行:"+ line); + } + } + if (count>0){ + double average=(double)sum/count; + System.out.println("平均分"+average); + } else { + System.out.println("没有有效成绩数据"); + } + } catch (FileNotFoundException e) { + System.out.println("错误:文件不存在-"+filepath); + } catch (IOException e) { + System.out.println("错误,读取文件时发生错误-"+e.getMessage()); + } + } +} \ No newline at end of file