1 changed files with 29 additions and 0 deletions
@ -0,0 +1,29 @@ |
|||||
|
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()); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue