From 0d4b743d88baf8cb7c7052d2747591bf3fe9774f Mon Sep 17 00:00:00 2001 From: Chengwuyi <3394813085@qq.com> Date: Sat, 30 May 2026 22:06:09 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6=E8=87=B3?= =?UTF-8?q?=20'w6'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- w6/Animal.java | 11 +++++++++++ w6/AnimalTest.java | 22 ++++++++++++++++++++++ w6/Cat.java | 13 +++++++++++++ w6/Dog.java | 18 ++++++++++++++++++ w6/ScoreReader.js | 45 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 109 insertions(+) create mode 100644 w6/Animal.java create mode 100644 w6/AnimalTest.java create mode 100644 w6/Cat.java create mode 100644 w6/Dog.java create mode 100644 w6/ScoreReader.js diff --git a/w6/Animal.java b/w6/Animal.java new file mode 100644 index 0000000..46a9869 --- /dev/null +++ b/w6/Animal.java @@ -0,0 +1,11 @@ +package org.example; + +public abstract class Animal { + String name; + + public Animal(String name) { + this.name = name; + } + + public abstract void makeSound(); +} \ No newline at end of file diff --git a/w6/AnimalTest.java b/w6/AnimalTest.java new file mode 100644 index 0000000..9089986 --- /dev/null +++ b/w6/AnimalTest.java @@ -0,0 +1,22 @@ +package org.example; + +public class AnimalTest { + public static void main(String[] args) { + + Animal dog = new Dog("Buddy"); + Animal cat = new Cat("Kitty"); + + dog.makeSound(); + cat.makeSound(); + + if (dog instanceof Swimmable) { + ((Swimmable) dog).swim(); + } + + if (cat instanceof Swimmable) { + ((Swimmable) cat).swim(); + } else { + System.out.println(cat.name + " cannot swim."); + } + } +} \ No newline at end of file diff --git a/w6/Cat.java b/w6/Cat.java new file mode 100644 index 0000000..df82e20 --- /dev/null +++ b/w6/Cat.java @@ -0,0 +1,13 @@ +package org.example; + +public class Cat extends Animal { + + public Cat(String name) { + super(name); + } + + @Override + public void makeSound() { + System.out.println(name + " says: Meow!"); + } +} \ No newline at end of file diff --git a/w6/Dog.java b/w6/Dog.java new file mode 100644 index 0000000..21621df --- /dev/null +++ b/w6/Dog.java @@ -0,0 +1,18 @@ +package org.example; + +public class Dog extends Animal implements Swimmable { + + public Dog(String name) { + super(name); + } + + @Override + public void makeSound() { + System.out.println(name + " says: Woof!"); + } + + @Override + public void swim() { + System.out.println(name + " is swimming!"); + } +} \ No newline at end of file diff --git a/w6/ScoreReader.js b/w6/ScoreReader.js new file mode 100644 index 0000000..2dbf16c --- /dev/null +++ b/w6/ScoreReader.js @@ -0,0 +1,45 @@ +package org.example; + +import java.io.BufferedReader; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; + +public class ScoreReader { + public static void main(String[] args) { + + int sum = 0; + int count = 0; + + // try-with-resources:自动关闭流,不需要手动 br.close() + try (BufferedReader br = new BufferedReader(new FileReader("scores.txt"))) { + + String line; + while ((line = br.readLine()) != null) { + try { + sum += Integer.parseInt(line.trim()); + count++; +} catch (NumberFormatException e) { + // 处理数字格式错误 + System.out.println("跳过无效数据:" + line); +} +} + +if (count > 0) { + double average = (double) sum / count; + System.out.println("总分:" + sum); + System.out.println("人数:" + count); + System.out.printf("平均分:%.2f%n", average); +} else { + System.out.println("文件中没有有效成绩。"); +} + +} catch (FileNotFoundException e) { + // 处理文件不存在 + System.out.println("错误:找不到文件 scores.txt,请检查路径。"); +} catch (IOException e) { + // 处理读取错误 + System.out.println("错误:读取文件时发生异常:" + e.getMessage()); +} +} +} \ No newline at end of file