3 changed files with 38 additions and 0 deletions
@ -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)); |
|||
} |
|||
} |
|||
|
After Width: | Height: | Size: 138 KiB |
|
After Width: | Height: | Size: 242 KiB |
Loading…
Reference in new issue