Browse Source

上传文件至 'w7'

main
yuangandong 4 weeks ago
parent
commit
3b325ba823
  1. 36
      w7/1
  2. BIN
      w7/截图.jpg

36
w7/1

@ -0,0 +1,36 @@
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class ScoreAverage {
public static void main() {
double sum = 0;
int count = 0;
double scoreAverage = 0;
try(BufferedReader br = new BufferedReader(new FileReader("scores.txt"))) {
String line;
while ((line = br.readLine()) != null) {
sum += Integer.parseInt(line);
count += 1;
}
if (count == 0){
System.out.println("文件中无有效数据");
}else{
scoreAverage = sum / count;
System.out.printf("由该文件中数据可得平均分为%.2f",scoreAverage);
}
} catch (FileNotFoundException e) {
System.out.println("文件不存在");
e.printStackTrace();
} catch (NumberFormatException e) {
System.out.println("数字格式错误");
e.printStackTrace();
} catch (IOException e) {
System.out.println("文件读取发生错误");
e.printStackTrace();
}
}
}

BIN
w7/截图.jpg

Binary file not shown.

After

Width:  |  Height:  |  Size: 380 KiB

Loading…
Cancel
Save