diff --git a/w7/1 b/w7/1 new file mode 100644 index 0000000..b13dbd1 --- /dev/null +++ b/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(); + } + } +} diff --git a/w7/截图.jpg b/w7/截图.jpg new file mode 100644 index 0000000..0d21833 Binary files /dev/null and b/w7/截图.jpg differ