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.
 
 

36 lines
1.1 KiB

import java.io.*;
public class ScoreCalculate {
public static void main(String[] args){
String filename="w7/Scores.txt";
int sum=0;
int count=0;
try(BufferedReader br=new BufferedReader(new FileReader(filename))){
String line;
while((line=br.readLine())!=null){
line=line.trim();
if(line.isEmpty()){
continue;
}
sum+=Integer.parseInt(line);
count++;
}
if(count>0){
double average=(double)sum/count;
System.out.println("平均分为"+average);
}
else{
System.out.println("无有效成绩");
}
}
catch(FileNotFoundException e){
System.out.println("文件未找到或不存在:"+e.getMessage());
}
catch(IOException e){
System.out.println("文件读取错误:"+e.getMessage());
}
catch(NumberFormatException e){
System.out.print("文件中包含无法解析为数字的内容:"+e.getMessage());
}
}
}