commit d013b3032066ed6f0a7cd271fceae6b1093a8d98 Author: LiZifan <1400341067@qq.com> Date: Tue Apr 28 10:49:15 2026 +0800 1 diff --git a/w1/README.md b/w1/README.md new file mode 100644 index 0000000..772fc38 --- /dev/null +++ b/w1/README.md @@ -0,0 +1,39 @@ +# 温度转换器 (Temperature Converter) + +这是一个简单的Java命令行程序,用于摄氏度(°C)与华氏度(°F)之间的相互转换。用户输入一个温度值和单位,程序自动进行转换并输出结果。 + +## 功能特点 + +- 支持摄氏度转华氏度 +- 支持华氏度转摄氏度 +- 友好的用户输入提示 +- 默认单位为摄氏度(当只输入数字时) +- 精确到小数点后两位的输出 + +## 环境要求 + +- **JDK版本**: 25.0.2 或更高版本(代码兼容Java 8+,但已在JDK 25.0.2下测试) +- **操作系统**: 任何支持Java的平台(Windows/Linux/macOS) + +## 如何运行 + +1. **编译代码** + 打开终端(命令提示符),进入代码所在目录,执行: + ```bash + javac TemperatureConverter.java +``` + +1. 运行程序 + 编译成功后,执行: + ```bash + java TemperatureConverter + ``` +2. 按照提示输入 + 例如: + ``` + 请输入要转换的温度与单位(例如 36.6 C 或 97 F):36.6 C + ``` + 输出: + ``` + 36.60 °C = 97.88 °F + ``` diff --git a/w1/TemperatureConverter.java b/w1/TemperatureConverter.java new file mode 100644 index 0000000..289983e --- /dev/null +++ b/w1/TemperatureConverter.java @@ -0,0 +1,68 @@ +import java.util.Scanner; + +/** + * 温度转换器示例程序(Java版) + * 支持摄氏度(C)与华氏度(F)之间互转 + */ +public class TemperatureConverter { + + /** + * 将摄氏度转换为华氏度。 + * + * @param c 摄氏温度 + * @return 对应的华氏温度 + */ + public static double celsiusToFahrenheit(double c) { + return c * 9.0 / 5.0 + 32.0; + } + + /** + * 将华氏度转换为摄氏度。 + * + * @param f 华氏温度 + * @return 对应的摄氏温度 + */ + public static double fahrenheitToCelsius(double f) { + return (f - 32.0) * 5.0 / 9.0; + } + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + + // 提示用户输入,格式示例:"36.6 C" 或 "97 F" + System.out.print("请输入要转换的温度与单位(例如 36.6 C 或 97 F):"); + String s = scanner.nextLine().trim(); + + if (s.isEmpty()) { + System.out.println("输入为空,程序退出。"); + return; + } + + String[] parts = s.split("\\s+"); + double value; + String unit; + + try { + value = Double.parseDouble(parts[0]); + // 允许用户输入两个部分:数值与单位,若单位缺失则默认为 'C' + unit = (parts.length > 1) ? parts[1].toUpperCase() : "C"; + } catch (NumberFormatException e) { + System.out.println("输入解析失败,请按示例输入数值与单位,例如:36.6 C"); + return; + } + + if (unit.startsWith("C")) { + // 从摄氏度转换为华氏度 + double f = celsiusToFahrenheit(value); + System.out.printf("%.2f °C = %.2f °F%n", value, f); + } else if (unit.startsWith("F")) { + // 从华氏度转换为摄氏度 + double c = fahrenheitToCelsius(value); + System.out.printf("%.2f °F = %.2f °C%n", value, c); + } else { + System.out.println("未知单位,请使用 C 或 F。"); + } + + scanner.close(); + } +} \ No newline at end of file diff --git a/w1/协助记录.txt b/w1/协助记录.txt new file mode 100644 index 0000000..e69de29 diff --git a/w1/屏幕截图 2026-03-09 101351.png b/w1/屏幕截图 2026-03-09 101351.png new file mode 100644 index 0000000..c25b720 Binary files /dev/null and b/w1/屏幕截图 2026-03-09 101351.png differ diff --git a/w2/w2.java b/w2/w2.java new file mode 100644 index 0000000..bd5809b --- /dev/null +++ b/w2/w2.java @@ -0,0 +1,35 @@ +public class w2{ + public static void main(String[] args) { + int[] sensorData = {85, -5, 92, 0, 105, 999, 88, 76}; + int validSum = 0; // 有效数据总和 + int validCount = 0; // 有效数据个数 + + // 遍历传感器数据数组 + for (int data : sensorData) { + // 规则3:致命错误(999),终止处理 + if (data == 999) { + System.out.println("致命错误:传感器掉线,终止处理。"); + break; + } + + // 规则2:无效数据(0/负数/大于100且非999),跳过并告警 + if (data <= 0 || data > 100) { + System.out.println("警告:发现越界数据[" + data + "],已跳过。"); + continue; + } + + // 规则1:正常数据(1-100),累加求和并计数 + validSum += data; + validCount++; + } + + // 最终输出逻辑 + if (validCount > 0) { + // 强制浮点除法,保留小数部分 + double average = (double) validSum / validCount; + System.out.println("有效数据平均值为:" + average); + } else { + System.out.println("无有效数据。"); + } + } +} diff --git a/w3/BankAccount.java b/w3/BankAccount.java new file mode 100644 index 0000000..cb506ac --- /dev/null +++ b/w3/BankAccount.java @@ -0,0 +1,52 @@ + +public class BankAccount { + private final String accountNumber; + private String ownerName; + private double balance; + public BankAccount(String accountNumber, String ownerName) { + this.accountNumber = accountNumber; + this.ownerName = ownerName; + this.balance = 0.0; + } + public String getAccountNumber() { + return accountNumber; + } + public String getOwnerName() { + return ownerName; + } + + public void setOwnerName(String ownerName) { + this.ownerName = ownerName; + } + public double getBalance() { + return balance; + } + public void deposit(double amount) { + if (amount > 0) { + balance += amount; + System.out.println("存款成功!当前余额:" + balance); + } else { + System.out.println("存款失败:存款金额必须大于0!"); + } + } + public void withdraw(double amount) { + if (amount <= 0) { + System.out.println("取款失败:取款金额必须大于0!"); + } else if (amount > balance) { + System.out.println("取款失败:余额不足!"); + } else { + balance -= amount; + System.out.println("取款成功!当前余额:" + balance); + } + } + public static void main(String[] args) { + BankAccount account = new BankAccount("622202123456789", "张三"); + System.out.println("账号:" + account.getAccountNumber()); + System.out.println("户主:" + account.getOwnerName()); + System.out.println("初始余额:" + account.getBalance()); + + account.deposit(500); + account.withdraw(200); + account.withdraw(400); + } +} \ No newline at end of file diff --git a/w4/ShapeAreaCalculator.java b/w4/ShapeAreaCalculator.java new file mode 100644 index 0000000..d88d89f --- /dev/null +++ b/w4/ShapeAreaCalculator.java @@ -0,0 +1,80 @@ +package src.main.w5; +// 抽象图形类 +abstract class Shape { + public abstract double getArea(); +} + +// 圆形类 +class Circle extends Shape { + private double radius; + + public Circle(double radius) { + this.radius = radius; + } + + @Override + public double getArea() { + return Math.PI * radius * radius; + } +} + +// 矩形类 +class Rectangle extends Shape { + private double width; + private double height; + + public Rectangle(double width, double height) { + this.width = width; + this.height = height; + } + + @Override + public double getArea() { + return width * height; + } +} + +// 三角形类 +class Triangle extends Shape { + private double base; + private double height; + + public Triangle(double base, double height) { + this.base = base; + this.height = height; + } + + @Override + public double getArea() { + return 0.5 * base * height; + } +} + +// 图形工具类 +class ShapeUtil { + public static void printArea(Shape shape) { + System.out.printf("该图形的面积为:%.2f%n", shape.getArea()); + } +} + +// 主程序入口 +public class ShapeAreaCalculator { + public static void main(String[] args) { + // 创建不同图形对象 + Shape circle = new Circle(2.0); + Shape rectangle = new Rectangle(3.0, 4.0); + Shape triangle = new Triangle(3.0, 4.0); + + // 统一调用工具类打印面积 + System.out.println("圆形:"); + ShapeUtil.printArea(circle); + + System.out.println("矩形:"); + ShapeUtil.printArea(rectangle); + + System.out.println("三角形:"); + ShapeUtil.printArea(triangle); + } +} + + diff --git a/w4/屏幕截图 2026-03-29 185330.png b/w4/屏幕截图 2026-03-29 185330.png new file mode 100644 index 0000000..f2cdfd6 Binary files /dev/null and b/w4/屏幕截图 2026-03-29 185330.png differ diff --git a/w4/类图 b/w4/类图 new file mode 100644 index 0000000..259a8cf --- /dev/null +++ b/w4/类图 @@ -0,0 +1,29 @@ +classDiagram + class Shape { + <> + +getArea() double + } + class Circle { + -radius: double + +Circle(radius: double) + +getArea() double + } + class Rectangle { + -width: double + -height: double + +Rectangle(width: double, height: double) + +getArea() double + } + class Triangle { + -base: double + -height: double + +Triangle(base: double, height: double) + +getArea() double + } + class ShapeUtil { + +printArea(Shape shape) void + } + Shape <|-- Circle + Shape <|-- Rectangle + Shape <|-- Triangle + ShapeUtil ..> Shape \ No newline at end of file diff --git a/w5/DuoTai.java b/w5/DuoTai.java new file mode 100644 index 0000000..e69de29 diff --git a/w6/AnimalTest.java b/w6/AnimalTest.java new file mode 100644 index 0000000..8ab4b78 --- /dev/null +++ b/w6/AnimalTest.java @@ -0,0 +1,56 @@ +package main.w6; + +// 1. 抽象动物类 +abstract class Animal { + // 抽象方法:动物叫声 + public abstract void makeSound(); +} + +// 2. 游泳接口 +interface Swimmable { + void swim(); +} + +// 3. Dog类:继承Animal,实现Swimmable接口 +class Dog extends Animal implements Swimmable { + @Override + public void makeSound() { + System.out.println("小狗汪汪叫~"); + } + + @Override + public void swim() { + System.out.println("小狗会狗刨游泳"); + } +} + +// 4. Cat类:仅继承Animal,不实现Swimmable +class Cat extends Animal { + @Override + public void makeSound() { + System.out.println("小猫喵喵叫~"); + } +} + +// 测试类 +public class AnimalTest { + public static void main(String[] args) { + // 多态调用:父类引用指向子类对象 + Animal dog = new Dog(); + Animal cat = new Cat(); + + // 调用叫声方法 + System.out.println("=== 动物叫声 ==="); + dog.makeSound(); + cat.makeSound(); + + // 只有实现了Swimmable的对象才能调用swim() + System.out.println("\n=== 游泳能力 ==="); + if (dog instanceof Swimmable) { + ((Swimmable) dog).swim(); + } + + // cat没有实现Swimmable,不能调用swim() + System.out.println("小猫不会游泳"); + } +} \ No newline at end of file diff --git a/w7/Pair.java b/w7/Pair.java new file mode 100644 index 0000000..472e13c --- /dev/null +++ b/w7/Pair.java @@ -0,0 +1,21 @@ +package w7; + +public class Pair { + private K key; + private V value; + public Pair(K key,V value){ + this.key=key; + this.value=value; + } + public K getKey(){ + return key; + } + public V getValue(){ + return value; + } + public static Pair swap(Pair pair) { + return new Pair<>(pair.getValue(), pair.getKey()); + } + +} + diff --git a/w7/Test.java b/w7/Test.java new file mode 100644 index 0000000..6fb57c6 --- /dev/null +++ b/w7/Test.java @@ -0,0 +1,13 @@ +package w7; + +public class Test { + public static void main(String[] args) { + // 原始 Pair + Pair original = new Pair<>("年龄", 18); + System.out.println("交换前:" + original); + + // 调用静态 swap 方法交换键值 + Pair swapped = Pair.swap(original); + System.out.println("交换后:" + swapped); + } +} \ No newline at end of file