ZhengJiayin 1 month ago
parent
commit
72d5ee9b1a
  1. BIN
      w7/ScoreCalculator.class
  2. 37
      w7/ScoreCalculator.java

BIN
w7/ScoreCalculator.class

Binary file not shown.

37
w7/ScoreCalculator.java

@ -0,0 +1,37 @@
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class ScoreCalculator {
public static void main(String[] args) {
String fileName = "scores.txt";
try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
String line;
int sum = 0;
int count = 0;
while ((line = br.readLine()) != null) {
try {
int score = Integer.parseInt(line.trim());
sum += score;
count++;
} catch (NumberFormatException e) {
System.err.println("Number format error, skip invalid line: " + line);
}
}
if (count > 0) {
double average = (double) sum / count;
System.out.println("Total: " + sum);
System.out.println("Count: " + count);
System.out.println("Average: " + average);
} else {
System.out.println("No valid score data");
}
} catch (IOException e) {
System.err.println("File read error: " + e.getMessage());
}
}
}
Loading…
Cancel
Save