diff --git a/w7/ScoreAverageCalculator.class b/w7/ScoreAverageCalculator.class new file mode 100644 index 0000000..b9e49cf Binary files /dev/null and b/w7/ScoreAverageCalculator.class differ diff --git a/w7/ScoreAverageCalculator.java b/w7/ScoreAverageCalculator.java new file mode 100644 index 0000000..e0568c4 --- /dev/null +++ b/w7/ScoreAverageCalculator.java @@ -0,0 +1,37 @@ +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; + +public class ScoreAverageCalculator { + public static void main(String[] args) { + int sum = 0; + int count = 0; + + // try-with-resources 自动关闭流 + try (BufferedReader br = new BufferedReader(new FileReader("scores.txt"))) { + String line; + while ((line = br.readLine()) != null) { + line = line.trim(); + if (line.isEmpty()) { + continue; + } + int score = Integer.parseInt(line); + sum += score; + count++; + } + + if (count > 0) { + double average = (double) sum / count; + System.out.printf("总分:%d,有效成绩数:%d,平均分:%.2f%n", sum, count, average); + } else { + System.out.println("文件无有效成绩!"); + } + } catch (java.io.FileNotFoundException e) { + System.out.println("错误:文件未找到或无法访问!" + e.getMessage()); + } catch (NumberFormatException e) { + System.out.println("错误:包含非数字数据!" + e.getMessage()); + } catch (IOException e) { + System.out.println("错误:读取文件失败!" + e.getMessage()); + } + } +} diff --git a/w7/scores.txt b/w7/scores.txt new file mode 100644 index 0000000..0c34947 --- /dev/null +++ b/w7/scores.txt @@ -0,0 +1,4 @@ +85 +92 +78 +66 \ No newline at end of file diff --git a/w7/杩愯鎴浘.png b/w7/杩愯鎴浘.png new file mode 100644 index 0000000..1dd9ee9 Binary files /dev/null and b/w7/杩愯鎴浘.png differ