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.
 
 
 

38 lines
1.2 KiB

package Homework;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Score {
public static void main(String[] args) {
String filepath="score.txt";
int sum=0;
int count=0;
double average=0.0;
try(BufferedReader br=new BufferedReader(new FileReader(filepath))){
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.out.println("数字格式错误");
}
}
if (count==0){
System.out.println("没有有效数据");
}else{
average=(double)sum/count;
System.out.println("平均分是:"+average);
System.out.println("人数是:"+count);
System.out.println("总分是:"+sum);
}
}catch(IOException e){
System.out.println("文件读取错误");
}
}
}