diff --git a/w7/ScoreAverageCalculator.java b/w7/ScoreAverageCalculator.java new file mode 100644 index 0000000..0439f21 --- /dev/null +++ b/w7/ScoreAverageCalculator.java @@ -0,0 +1,48 @@ +package Score; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; +public class ScoreAverageCalculator { + public static void main(String[] args) { + String filePath = "scores.txt"; + int sum = 0; + int count = 0; + + // try-with-resources 确保 BufferedReader 自动关闭 + try (BufferedReader br = new BufferedReader(new FileReader(filePath))) { + String line; + while ((line = br.readLine()) != null) { + // 跳过空行 + if (line.trim().isEmpty()) { + continue; + } + try { + int score = Integer.parseInt(line.trim()); + sum += score; + count++; + } catch (NumberFormatException e) { + // 数字格式错误处理:打印错误信息并跳过该行 + System.err.println("数字格式错误,跳过无效数据: " + line); + } + } + + // 计算并输出平均分 + if (count > 0) { + double average = (double) sum / count; + System.out.println("有效成绩数量: " + count); + System.out.println("成绩总和: " + sum); + System.out.println("平均分: " + average); + } else { + System.out.println("没有有效的成绩数据"); + } + + } catch (IOException e) { + // 文件不存在或读取错误处理 + System.err.println("文件操作错误: " + e.getMessage()); + if (e instanceof java.io.FileNotFoundException) { + System.err.println("请确认文件 " + filePath + " 是否存在"); + } + } + } +} diff --git a/w7/ai使用记录.txt b/w7/ai使用记录.txt new file mode 100644 index 0000000..06d7405 Binary files /dev/null and b/w7/ai使用记录.txt differ diff --git a/w7/e77000dc9d781471beb6f69e49f38d09.png b/w7/e77000dc9d781471beb6f69e49f38d09.png new file mode 100644 index 0000000..17655dd Binary files /dev/null and b/w7/e77000dc9d781471beb6f69e49f38d09.png differ diff --git a/w7/ed21b8be723dd640d426916d974648dd.png b/w7/ed21b8be723dd640d426916d974648dd.png new file mode 100644 index 0000000..b4183c5 Binary files /dev/null and b/w7/ed21b8be723dd640d426916d974648dd.png differ