From fe27490ea18a0ed4692ea6b620d41c1da97de0f6 Mon Sep 17 00:00:00 2001 From: WangHe <2260378191@qq.com> Date: Sat, 7 Mar 2026 12:26:17 +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'exercise1'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 第一次作业 --- exercise1/AI_help.txt | 21 +++++ exercise1/README.md | 28 ++++++ exercise1/TemperatureConverter.java | 134 ++++++++++++++++++++++++++++ exercise1/temps.txt | 35 ++++++++ 4 files changed, 218 insertions(+) create mode 100644 exercise1/AI_help.txt create mode 100644 exercise1/README.md create mode 100644 exercise1/TemperatureConverter.java create mode 100644 exercise1/temps.txt diff --git a/exercise1/AI_help.txt b/exercise1/AI_help.txt new file mode 100644 index 0000000..362b80d --- /dev/null +++ b/exercise1/AI_help.txt @@ -0,0 +1,21 @@ +请将以下 Python 温度转换程序改写为功能完全等效的 Java 程序。 + +要求: +1. 保留所有原有功能和逻辑(包括默认单位为摄氏、输入解析、错误处理等) +2. 保留并适当扩展中文注释(方法功能、参数说明、主要逻辑说明) +3. 代码风格清晰、符合 Java 常见规范(命名、缩进、异常处理等) +4. 使用 Scanner 处理交互输入 +5. 输出格式统一美观(建议使用 printf,保留两位小数,带 °C/°F 符号) + +可选加分项(按优先级排序): +- 支持命令行参数模式(例:java TemperatureConverter 36.6 C) +- 支持批量文件转换(例:java TemperatureConverter input.txt,每行一条温度数据) +- 在批量模式中添加简易控制台进度条(覆盖刷新式,显示百分比和进度字符) + +额外输出要求: +- 完整的 Java 源代码(文件名建议:TemperatureConverter.java) +- 一个规范的 README.md 文件,包含: + - 项目简介 + - 编译 & 运行命令(考虑 Windows 编码问题) + - 三种使用方式的详细示例(交互、单次参数、批量文件) + - 典型输入输出样例 \ No newline at end of file diff --git a/exercise1/README.md b/exercise1/README.md new file mode 100644 index 0000000..b4b803f --- /dev/null +++ b/exercise1/README.md @@ -0,0 +1,28 @@ +# 温度转换器(Java 增强版) + +一个简单但功能完备的控制台温度转换工具,支持 **摄氏度 (℃) ↔ 华氏度 (℉)** 互转。 + +支持 **三种使用方式**: + +1. 交互式(命令行逐行输入) +2. 单次命令行参数(适合快速转换) +3. 批量文件处理(适合一次性转换多条数据) + +## 功能特性 + +- 支持默认单位(无单位时视为 ℃) +- 保留两位小数输出 +- 友好错误提示(非法数值、未知单位、空行等) +- 支持负数温度 +- UTF-8 友好(中文提示无乱码) + +## 编译与运行 + +需要 **JDK 8 或更高版本**(推荐 JDK 11+ / 17 / 21) + +```bash +# 1. 编译 +javac TemperatureConverter.java + +# 2. 运行(推荐显式指定 UTF-8 编码,避免 Windows cmd 乱码) +java -Dfile.encoding=UTF-8 TemperatureConverter \ No newline at end of file diff --git a/exercise1/TemperatureConverter.java b/exercise1/TemperatureConverter.java new file mode 100644 index 0000000..b5f81ed --- /dev/null +++ b/exercise1/TemperatureConverter.java @@ -0,0 +1,134 @@ +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; +import java.util.Scanner; + +/** + * 温度转换器示例程序(Java) - 增强版 + * 支持摄氏度(C) ↔ 华氏度(F) 互转 + * 支持三种使用方式: + * 1. 交互模式(不带参数) + * 2. 单次命令行参数模式:java TemperatureConverter 36.6 C + * 3. 批量文件模式:java TemperatureConverter input.txt + */ +public class TemperatureConverter { + + /** + * 将摄氏度转换为华氏度 + * 公式:F = C × 9/5 + 32 + * + * @param c 摄氏温度 + * @return 华氏温度 + */ + public static float celsiusToFahrenheit(float c) { + return c * 9.0f / 5.0f + 32.0f; + } + + /** + * 将华氏度转换为摄氏度 + * 公式:C = (F - 32) × 5/9 + * + * @param f 华氏温度 + * @return 摄氏温度 + */ + public static float fahrenheitToCelsius(float f) { + return (f - 32.0f) * 5.0f / 9.0f; + } + + /** + * 解析单行温度字符串并返回转换结果(字符串形式) + * + * @param line 一行输入,例如 "36.6 C" 或 "98.6" 或 "99 F" + * @return 转换结果字符串,或错误信息 + */ + public static String convertLine(String line) { + if (line == null || line.trim().isEmpty()) { + return null; + } + + String[] parts = line.trim().split("\\s+"); + float value; + String unit = "C"; // 默认摄氏 + + try { + value = Float.parseFloat(parts[0]); + if (parts.length > 1) { + unit = parts[1].trim().toUpperCase(); + } + } catch (NumberFormatException e) { + return "格式错误: " + line; + } + + if (unit.startsWith("C")) { + float f = celsiusToFahrenheit(value); + return String.format("%.2f °C → %.2f °F", value, f); + } else if (unit.startsWith("F")) { + float c = fahrenheitToCelsius(value); + return String.format("%.2f °F → %.2f °C", value, c); + } else { + return "未知单位: " + line; + } + } + + public static void main(String[] args) { + // 1. 命令行单次转换模式 + if (args.length == 2) { + String input = args[0] + " " + args[1]; + String result = convertLine(input); + if (result != null) { + System.out.println(result); + } + return; + } + + // 2. 批量文件模式 + if (args.length == 1) { + String filename = args[0]; + System.out.println("正在处理文件: " + filename); + System.out.println("----------------------------------------"); + + try (BufferedReader br = new BufferedReader(new FileReader(filename))) { + String line; + int lineNum = 0; + while ((line = br.readLine()) != null) { + lineNum++; + String result = convertLine(line); + if (result != null) { + System.out.printf("第 %2d 行: %s%n", lineNum, result); + } + } + System.out.println("----------------------------------------"); + System.out.println("处理完成,共读取 " + lineNum + " 行"); + } catch (IOException e) { + System.err.println("无法读取文件: " + filename); + System.err.println(e.getMessage()); + } + return; + } + + // 3. 交互模式(无参数) + Scanner scanner = new Scanner(System.in); + System.out.println("温度转换器(支持 C / F)"); + System.out.println("输入格式示例:36.6 C 或 98.6 F 或 23.5(默认C)"); + System.out.println("输入空行或 Ctrl+C 退出\n"); + + while (true) { + System.out.print("> "); + String line = scanner.nextLine().trim(); + + if (line.isEmpty()) { + System.out.println("再见!"); + break; + } + + String result = convertLine(line); + if (result != null) { + System.out.println(result); + } else { + System.out.println("(已跳过空行)"); + } + } + + scanner.close(); + } +} \ No newline at end of file diff --git a/exercise1/temps.txt b/exercise1/temps.txt new file mode 100644 index 0000000..360006e --- /dev/null +++ b/exercise1/temps.txt @@ -0,0 +1,35 @@ +input +36.6 C +98.6 F +0 C +32 F +100 C +212 F +-40 C +-40 F +25.5 +37.2 c +101.3 f +abc +23.5 K +88.5 + 42.0 F +19.8C + +output +36.60 °C → 97.88 °F +98.60 °F → 37.00 °C +0.00 °C → 32.00 °F +32.00 °F → 0.00 °C +100.00 °C → 212.00 °F +212.00 °F → 100.00 °C +-40.00 °C → -40.00 °F +-40.00 °F → -40.00 °C +25.50 °C → 77.90 °F (默认 C) +37.20 °C → 98.96 °F +101.30 °F → 38.50 °C +格式错误: abc +未知单位: 23.5 K +88.50 °C → 191.30 °F +42.00 °F → 5.56 °C +19.80 °C → 67.64 °F \ No newline at end of file