You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
29 lines
1.1 KiB
29 lines
1.1 KiB
public class ScoreAvg {
|
|
public static void main(String[] args) {
|
|
int sum = 0;
|
|
int count = 0;
|
|
double average = 0;
|
|
try (java.io.BufferedReader br = new java.io.BufferedReader(new java.io.FileReader("scores.txt"))) {
|
|
String line;
|
|
while ((line = br.readLine()) != null) {
|
|
try {
|
|
int score = Integer.parseInt(line.trim());
|
|
sum += score;
|
|
count++;
|
|
} catch (NumberFormatException e) {
|
|
System.out.println("格式错误跳过该行" + line);
|
|
}
|
|
}
|
|
if (count > 0) {
|
|
average = (double) sum / count;
|
|
System.out.println("总分:" + sum);
|
|
System.out.println("人数:" + count);
|
|
System.out.println("平均分:" + average);
|
|
} else {
|
|
System.out.println("文件内无有效数据");
|
|
}
|
|
} catch (java.io.IOException e) {
|
|
System.out.println("文件操作出错:" + e.getMessage());
|
|
}
|
|
}
|
|
}
|