commit 4625269f813334f342094ad5f114ab61723fc08a Author: LiZifan <1400341067@qq.com> Date: Mon Mar 23 11:01:17 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