Browse Source

上传文件至 'w7'

main
wangbo 2 months ago
parent
commit
b62b284424
  1. 38
      w7/ScoreCalculator.java
  2. BIN
      w7/屏幕截图 2026-04-18 210235.png
  3. BIN
      w7/屏幕截图 2026-04-18 210248.png

38
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));
}
}

BIN
w7/屏幕截图 2026-04-18 210235.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 138 KiB

BIN
w7/屏幕截图 2026-04-18 210248.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 242 KiB

Loading…
Cancel
Save