8 changed files with 51 additions and 0 deletions
|
After Width: | Height: | Size: 9.8 KiB |
|
After Width: | Height: | Size: 9.0 KiB |
|
After Width: | Height: | Size: 2.1 KiB |
@ -0,0 +1,15 @@ |
|||||
|
import java.io.*; |
||||
|
|
||||
|
public class Raw { |
||||
|
public static void main(String[] args){ |
||||
|
String filename="w7/Score.txt"; |
||||
|
int sum=0; |
||||
|
int count=0; |
||||
|
BufferedReader br=new BufferedReader(new FileReader(filename)); |
||||
|
String line; |
||||
|
while((line= br.readLine())!=null){ |
||||
|
sum+=Integer.parseInt(line); |
||||
|
count++; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,36 @@ |
|||||
|
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()); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
After Width: | Height: | Size: 12 KiB |
|
After Width: | Height: | Size: 3.5 KiB |
Loading…
Reference in new issue