Compare commits
2 Commits
81f8a0b77f
...
75a1c3432f
| Author | SHA1 | Date |
|---|---|---|
|
|
75a1c3432f | 2 months ago |
|
|
02420ec4bd | 2 months ago |
1 changed files with 38 additions and 0 deletions
@ -0,0 +1,38 @@ |
|||||
|
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("文件读取错误"); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue