diff --git a/w1/AI协助记录.txt b/w1/AI协助记录.txt new file mode 100644 index 0000000..98a1ca0 --- /dev/null +++ b/w1/AI协助记录.txt @@ -0,0 +1 @@ + 在完成本次温度转换作业时,我先使用AI帮助解析作业要求,把任务描述、提交规范、评估标准等内容翻译成更通俗的理解,明确需要完成哪些文件、达到什么效果。之后在Python转Java的过程中,AI为我提供了代码结构思路与格式参考。在环境配置、编码报错、编译运行等问题上,AI给出了具体可操作的解决步骤。 \ No newline at end of file diff --git a/w1/README.md b/w1/README.md new file mode 100644 index 0000000..fc9593e --- /dev/null +++ b/w1/README.md @@ -0,0 +1,12 @@ +\# 温度转换程序 + +功能:摄氏温度与华氏温度相互转换 + +编译命令: + +javac TemperatureConverter.java + +运行命令: + +java TemperatureConverter + diff --git a/w1/TemperatureConverter.class b/w1/TemperatureConverter.class new file mode 100644 index 0000000..6fe6c19 Binary files /dev/null and b/w1/TemperatureConverter.class differ diff --git a/w1/TemperatureConverter.java b/w1/TemperatureConverter.java new file mode 100644 index 0000000..fea6f8d --- /dev/null +++ b/w1/TemperatureConverter.java @@ -0,0 +1,63 @@ +import java.util.Scanner; + +// 温度转换:摄氏 ↔ 华氏 +public class TemperatureConverter { + // 摄氏转华氏 + public static double c2f(double c) { + return c * 9 / 5 + 32; + } + // 华氏转摄氏 + public static double f2c(double f) { + return (f - 32) * 5 / 9; + } + + public static void main(String[] args) { + if (args.length >= 1) { + runWithArgs(args); + return; + } + Scanner sc = new Scanner(System.in); + System.out.print("输入温度和单位(如 36.6 C 或 97 F):"); + String line = sc.nextLine().trim(); + sc.close(); + if (line.isEmpty()) { + System.out.println("输入为空,退出。"); + return; + } + String[] parts = line.split(" "); + try { + double val = Double.parseDouble(parts[0]); + String unit = parts.length > 1 ? parts[1].toUpperCase() : "C"; + if (unit.startsWith("C")) { + double f = c2f(val); + System.out.printf("%.2f °C = %.2f °F%n", val, f); + } else if (unit.startsWith("F")) { + double c = f2c(val); + System.out.printf("%.2f °F = %.2f °C%n", val, c); + } else { + System.out.println("单位不对,用 C 或 F。"); + } + } catch (NumberFormatException e) { + System.out.println("输入不对,像这样:36.6 C"); + } + } + + // 处理命令行参数 + private static void runWithArgs(String[] args) { + try { + double val = Double.parseDouble(args[0]); + String unit = args.length > 1 ? args[1].toUpperCase() : "C"; + if (unit.startsWith("C")) { + double f = c2f(val); + System.out.printf("%.2f °C = %.2f °F%n", val, f); + } else if (unit.startsWith("F")) { + double c = f2c(val); + System.out.printf("%.2f °F = %.2f °C%n", val, c); + } else { + System.out.println("单位不对,用 C 或 F。"); + } + } catch (NumberFormatException e) { + System.out.println("参数不对,像这样:java TemperatureConverter 36.6 C"); + } + } +} \ No newline at end of file diff --git a/w1/运行截图.png b/w1/运行截图.png new file mode 100644 index 0000000..e17e2c4 Binary files /dev/null and b/w1/运行截图.png differ