diff --git a/w1/AI协助报告 b/w1/AI协助报告 new file mode 100644 index 0000000..7afe45f --- /dev/null +++ b/w1/AI协助报告 @@ -0,0 +1,2 @@ +使用的 Prompt:将 Python 温度转换程序完整移植为 Java 程序,文件名为 TemperatureConverter.java,保留原有注释并补充方法级中文注释,支持命令行参数模式(如 java TemperatureConverter 36.6 C),同时提供编译运行命令、README.md 模板、程序运行输出示例和符合要求的 AI 协助记录,满足课后作业提交规范。 +AI 提供的帮助:1. 按 Python 原逻辑编写 Java 温度转换核心方法,校验摄氏 / 华氏转换公式的正确性;2. 补充类、方法的中文注释,明确参数与功能;3. 实现命令行参数和交互两种运行模式,添加异常处理;4. 提供 README.md 的编译运行命令及运行示例; \ No newline at end of file diff --git a/w1/README.md b/w1/README.md new file mode 100644 index 0000000..65c749d --- /dev/null +++ b/w1/README.md @@ -0,0 +1,8 @@ +# 温度转换程序(Java版) +## 功能说明 +实现摄氏温度(C)与华氏温度(F)的双向转换,支持命令行参数和交互两种运行模式。 + +## 编译命令 +在程序所在目录执行: +```bash +javac TemperatureConverter.java \ No newline at end of file diff --git a/w1/TemperatureConverter.java b/w1/TemperatureConverter.java new file mode 100644 index 0000000..6fa20b2 --- /dev/null +++ b/w1/TemperatureConverter.java @@ -0,0 +1,82 @@ +import java.util.Scanner; + +/** + * 温度转换工具类 + * 功能:实现摄氏温度(C)与华氏温度(F)之间的双向转换 + * 转换公式: + * - 摄氏转华氏:F = C × 9/5 + 32 + * - 华氏转摄氏:C = (F - 32) × 5/9 + */ +public class TemperatureConverter { + + /** + * 摄氏温度转换为华氏温度 + * @param celsius 摄氏温度值(double类型) + * @return 转换后的华氏温度值(double类型) + */ + public static double celsiusToFahrenheit(double celsius) { + return celsius * 9.0 / 5.0 + 32; + } + + /** + * 华氏温度转换为摄氏温度 + * @param fahrenheit 华氏温度值(double类型) + * @return 转换后的摄氏温度值(double类型) + */ + public static double fahrenheitToCelsius(double fahrenheit) { + return (fahrenheit - 32) * 5.0 / 9.0; + } + + /** + * 主方法:程序入口 + * 支持两种运行模式: + * 1. 命令行参数模式:java TemperatureConverter 数值 单位(如 C/F) + * 2. 交互模式:无命令行参数时,控制台输入温度和单位 + * @param args 命令行参数数组 + */ + public static void main(String[] args) { + // 处理命令行参数模式 + if (args.length == 2) { + try { + double tempValue = Double.parseDouble(args[0]); + String unit = args[1].toUpperCase(); // 统一转为大写,兼容小写输入 + + if (unit.equals("C")) { + double fahrenheit = celsiusToFahrenheit(tempValue); + System.out.printf("%.2f 摄氏度 = %.2f 华氏度%n", tempValue, fahrenheit); + } else if (unit.equals("F")) { + double celsius = fahrenheitToCelsius(tempValue); + System.out.printf("%.2f 华氏度 = %.2f 摄氏度%n", tempValue, celsius); + } else { + System.err.println("单位错误!请输入 C(摄氏)或 F(华氏)"); + } + } catch (NumberFormatException e) { + System.err.println("温度值格式错误!请输入有效的数字"); + } + return; + } + + // 交互模式(无命令行参数时) + Scanner scanner = new Scanner(System.in); + System.out.println("=== 温度转换程序 ==="); + System.out.print("请输入温度值:"); + double tempValue = scanner.nextDouble(); + scanner.nextLine(); // 吸收换行符 + + System.out.print("请输入温度单位(C=摄氏,F=华氏):"); + String unit = scanner.nextLine().toUpperCase(); + + // 根据单位执行转换并输出结果 + if (unit.equals("C")) { + double fahrenheit = celsiusToFahrenheit(tempValue); + System.out.printf("转换结果:%.2f 摄氏度 = %.2f 华氏度%n", tempValue, fahrenheit); + } else if (unit.equals("F")) { + double celsius = fahrenheitToCelsius(tempValue); + System.out.printf("转换结果:%.2f 华氏度 = %.2f 摄氏度%n", tempValue, celsius); + } else { + System.out.println("输入的单位无效!请重新运行程序并输入 C 或 F"); + } + + scanner.close(); + } +} \ No newline at end of file diff --git a/w1/e9a62f990602eeabc560d992aa20469b.png b/w1/e9a62f990602eeabc560d992aa20469b.png new file mode 100644 index 0000000..d75f532 Binary files /dev/null and b/w1/e9a62f990602eeabc560d992aa20469b.png differ