diff --git a/w1/AI协助记录.txt b/w1/AI协助记录.txt new file mode 100644 index 0000000..8d80de0 --- /dev/null +++ b/w1/AI协助记录.txt @@ -0,0 +1 @@ +AI为我协助完成了Python到Java的代码转换,解释了Java中Scanner类的使用方法。当编译出现"找不到符号"错误时,AI指导添加了import语句解决了问题。AI还提供了命令行参数解析和批量文件处理的实现方案,并帮助编写了README文档和运行说明。 \ No newline at end of file diff --git a/w1/README.md b/w1/README.md new file mode 100644 index 0000000..8011653 --- /dev/null +++ b/w1/README.md @@ -0,0 +1,8 @@ +# 温度转换器 (TemperatureConverter) + +这是一个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..f0a0933 --- /dev/null +++ b/w1/TemperatureConverter.java @@ -0,0 +1,159 @@ +/** + * 温度转换器程序(Java版) + * 支持摄氏度(C)与华氏度(F)之间互转 + */ +import java.util.Scanner; +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; +import java.io.FileNotFoundException; + +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 interactiveMode() { + Scanner scanner = new Scanner(System.in); + + // 提示用户输入,格式示例:"36.6 C" 或 "97 F" + System.out.print("请输入要转换的温度与单位(例如 36.6 C 或 97 F):"); + String input = scanner.nextLine().trim(); + + if (input.isEmpty()) { + System.out.println("输入为空,程序退出。"); + scanner.close(); + return; + } + + processInput(input); + scanner.close(); + } + + /** + * 处理输入的字符串并输出转换结果 + * + * @param input 输入的字符串 + */ + public static void processInput(String input) { + String[] parts = input.split("\\s+"); + + try { + // 解析数值和单位 + double value = Double.parseDouble(parts[0]); + String unit = (parts.length > 1) ? parts[1].toUpperCase() : "C"; + + if (unit.startsWith("C")) { + // 从摄氏度转换为华氏度 + double f = celsiusToFahrenheit(value); + System.out.printf("%.1f °C = %.2f °F%n", value, f); + } else if (unit.startsWith("F")) { + // 从华氏度转换为摄氏度 + double c = fahrenheitToCelsius(value); + System.out.printf("%.1f °F = %.2f °C%n", value, c); + } else { + System.out.println("未知单位,请使用 C 或 F。"); + } + } catch (NumberFormatException e) { + System.out.println("输入解析失败,请按示例输入数值与单位,例如:36.6 C"); + } + } + + /** + * 命令行参数模式 + * + * @param args 命令行参数 + */ + public static void commandLineMode(String[] args) { + if (args.length < 1) { + System.out.println("请提供温度数值和单位,例如:36.6 C"); + return; + } + + try { + double value = Double.parseDouble(args[0]); + String unit = (args.length > 1) ? args[1].toUpperCase() : "C"; + + if (unit.startsWith("C")) { + double f = celsiusToFahrenheit(value); + System.out.printf("%.1f °C = %.2f °F%n", value, f); + } else if (unit.startsWith("F")) { + double c = fahrenheitToCelsius(value); + System.out.printf("%.1f °F = %.2f °C%n", value, c); + } else { + System.out.println("未知单位,请使用 C 或 F。"); + } + } catch (NumberFormatException e) { + System.out.println("参数解析失败,请提供有效的数值。"); + } + } + + /** + * 批量文件处理模式 + * + * @param filename 输入文件名 + */ + public static void batchMode(String filename) { + try { + BufferedReader reader = new BufferedReader(new FileReader(filename)); + String line; + int lineNumber = 0; + + System.out.println("批量转换结果:"); + System.out.println("================"); + + while ((line = reader.readLine()) != null) { + lineNumber++; + line = line.trim(); + if (line.isEmpty() || line.startsWith("#")) { + continue; // 跳过空行和注释行 + } + + System.out.print("第" + lineNumber + "行: "); + processInput(line); + } + + reader.close(); + } catch (FileNotFoundException e) { + System.out.println("文件未找到: " + filename); + } catch (IOException e) { + System.out.println("读取文件时出错: " + e.getMessage()); + } + } + + public static void main(String[] args) { + if (args.length > 0) { + // 命令行参数模式 + if (args[0].equals("-batch") && args.length > 1) { + // 批量文件处理模式 + batchMode(args[1]); + } else { + // 普通命令行参数模式 + commandLineMode(args); + } + } else { + // 交互模式 + interactiveMode(); + } + } +} \ No newline at end of file diff --git a/w1/程序运行截图.png b/w1/程序运行截图.png new file mode 100644 index 0000000..44771c3 Binary files /dev/null and b/w1/程序运行截图.png differ