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.

37 lines
1.2 KiB

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class ScoreAverage {
public static void main() {
double sum = 0;
int count = 0;
double scoreAverage = 0;
try(BufferedReader br = new BufferedReader(new FileReader("scores.txt"))) {
String line;
while ((line = br.readLine()) != null) {
sum += Integer.parseInt(line);
count += 1;
}
if (count == 0){
System.out.println("文件中无有效数据");
}else{
scoreAverage = sum / count;
System.out.printf("由该文件中数据可得平均分为%.2f",scoreAverage);
}
} catch (FileNotFoundException e) {
System.out.println("文件不存在");
e.printStackTrace();
} catch (NumberFormatException e) {
System.out.println("数字格式错误");
e.printStackTrace();
} catch (IOException e) {
System.out.println("文件读取发生错误");
e.printStackTrace();
}
}
}