今天(11.3)立信长沙笔试题
在以txt文件中纪录了如下信息
学号 姓名 成绩
1 ss 80
2 pp 95
…. ………. ……..
写一个你认为最高效的程序求出学生平均成绩(上面的信息存放在表格里的)
在以txt文件中纪录了如下信息
学号 姓名 成绩
1 ss 80
2 pp 95
…. ………. ……..
写一个你认为最高效的程序求出学生平均成绩(上面的信息存放在表格里的)
static int num = 0;
static int sum = 0;
public static void main(String args[]){
File file = new File("C:\\JavaCode\\Score.txt");
try{
BufferedReader read = new BufferedReader(new FileReader(file)); String text = read.readLine();
while(text != null){
text = text.trim();
if (text.matches("[0-9]+.*")){ //判断这一行有学号
num ++;
sum += Integer.parseInt(text.replaceAll("\\d+\\D+", ""));//把学好和姓名空格都替换掉,只留成绩
}
text = read.readLine();
}
System.out.print((double)sum/num);
}
catch(FileNotFoundException e){
e.printStackTrace();
}
catch(IOException e){
e.printStackTrace();
}
}
}
学号 姓名 成绩
1 ss 80
2 pp 95
3 p2 75
4 p3 84
5 pr 91
6 1p 85
7 ep 66答案是:82.28571...