diff --git a/AI协助记录 b/AI协助记录 new file mode 100644 index 0000000..10cd427 --- /dev/null +++ b/AI协助记录 @@ -0,0 +1,4 @@ +AI 协助记录: +我使用了以下 prompt: +“请将下面的 Python 温度转换程序改写为等效的 Java 程序,保留注释并保证功能一致。同时支持命令行参数模式和批量文件读取转换。” +AI 给出了完整的 Java 代码,并解释了每个方法的作用。我根据 AI 的建议添加了三种运行模式并整理了 README 和运行示例,确保符合提交要求。 diff --git a/README.md b/README.md index 4f264e5..eff3bb1 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,8 @@ -# 温度转换器 -将 Python 温度转换程序移植为 Java,支持摄氏度与华氏度互转。 -实现了三种运行模式:交互式、命令行参数、批量文件处理。 - -## 编译 -```bash +# 温度转换器 (Java 版) + +将 Python 温度转换程序移植为 Java,支持摄氏度与华氏度互转。 +实现了三种运行模式:交互式、命令行参数、批量文件处理。 + +## 编译 +```bash javac TemperatureConverter.java \ No newline at end of file diff --git a/TemperatureConverter.java b/TemperatureConverter.java index eae07a0..c8b6f34 100644 --- a/TemperatureConverter.java +++ b/TemperatureConverter.java @@ -1,116 +1,116 @@ -import java.io.*; -import java.util.*; - -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; - } - - /** - * 交互式模式:从标准输入读取一行,解析并转换。 - */ - private static void interactiveMode() { - Scanner scanner = new Scanner(System.in); - System.out.print("请输入要转换的温度与单位(例如 36.6 C 或 97 F):"); - String line = scanner.nextLine().trim(); - if (line.isEmpty()) { - System.out.println("输入为空,程序退出。"); - return; - } - processInputLine(line); - } - - /** - * 处理单行输入字符串,格式如 "36.6 C" 或 "97 F"。 - * 若解析成功则输出转换结果,否则打印错误信息。 - */ - private static void processInputLine(String line) { - String[] parts = line.split("\\s+"); - if (parts.length == 0) return; - try { - double value = Double.parseDouble(parts[0]); - String unit = (parts.length > 1) ? parts[1].toUpperCase() : "C"; - convertAndPrint(value, unit); - } catch (NumberFormatException e) { - System.out.println("输入解析失败,请按示例输入数值与单位,例如:36.6 C"); - } - } - - /** - * 根据数值和单位进行转换并打印结果。 - */ - private static void convertAndPrint(double value, String unit) { - 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。"); - } - } - - /** - * 命令行参数模式:两个参数,分别为数值和单位。 - */ - private static void commandLineMode(String valueStr, String unitStr) { - try { - double value = Double.parseDouble(valueStr); - String unit = unitStr.toUpperCase(); - convertAndPrint(value, unit); - } catch (NumberFormatException e) { - System.out.println("无效的数值参数,请提供正确的数字。"); - } - } - - /** - * 批量文件模式:从指定文件逐行读取,每行格式同交互输入。 - */ - private static void batchFileMode(String fileName) { - try (Scanner fileScanner = new Scanner(new File(fileName))) { - while (fileScanner.hasNextLine()) { - String line = fileScanner.nextLine().trim(); - if (line.isEmpty()) continue; - processInputLine(line); - } - } catch (FileNotFoundException e) { - System.out.println("文件未找到: " + fileName); - } - } - - public static void main(String[] args) { - if (args.length == 0) { - // 无参数:交互模式 - interactiveMode(); - } else if (args.length == 2) { - // 两个参数:命令行模式 - commandLineMode(args[0], args[1]); - } else if (args.length == 1) { - // 一个参数:视为文件名,进行批量转换 - batchFileMode(args[0]); - } else { - System.out.println("用法:"); - System.out.println(" 交互模式: java TemperatureConverter"); - System.out.println(" 单次转换: java TemperatureConverter <数值> <单位(C/F)>"); - System.out.println(" 批量转换: java TemperatureConverter <文件名>"); - } - } +import java.io.*; +import java.util.*; + +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; + } + + /** + * 交互式模式:从标准输入读取一行,解析并转换。 + */ + private static void interactiveMode() { + Scanner scanner = new Scanner(System.in); + System.out.print("请输入要转换的温度与单位(例如 36.6 C 或 97 F):"); + String line = scanner.nextLine().trim(); + if (line.isEmpty()) { + System.out.println("输入为空,程序退出。"); + return; + } + processInputLine(line); + } + + /** + * 处理单行输入字符串,格式如 "36.6 C" 或 "97 F"。 + * 若解析成功则输出转换结果,否则打印错误信息。 + */ + private static void processInputLine(String line) { + String[] parts = line.split("\\s+"); + if (parts.length == 0) return; + try { + double value = Double.parseDouble(parts[0]); + String unit = (parts.length > 1) ? parts[1].toUpperCase() : "C"; + convertAndPrint(value, unit); + } catch (NumberFormatException e) { + System.out.println("输入解析失败,请按示例输入数值与单位,例如:36.6 C"); + } + } + + /** + * 根据数值和单位进行转换并打印结果。 + */ + private static void convertAndPrint(double value, String unit) { + 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。"); + } + } + + /** + * 命令行参数模式:两个参数,分别为数值和单位。 + */ + private static void commandLineMode(String valueStr, String unitStr) { + try { + double value = Double.parseDouble(valueStr); + String unit = unitStr.toUpperCase(); + convertAndPrint(value, unit); + } catch (NumberFormatException e) { + System.out.println("无效的数值参数,请提供正确的数字。"); + } + } + + /** + * 批量文件模式:从指定文件逐行读取,每行格式同交互输入。 + */ + private static void batchFileMode(String fileName) { + try (Scanner fileScanner = new Scanner(new File(fileName))) { + while (fileScanner.hasNextLine()) { + String line = fileScanner.nextLine().trim(); + if (line.isEmpty()) continue; + processInputLine(line); + } + } catch (FileNotFoundException e) { + System.out.println("文件未找到: " + fileName); + } + } + + public static void main(String[] args) { + if (args.length == 0) { + // 无参数:交互模式 + interactiveMode(); + } else if (args.length == 2) { + // 两个参数:命令行模式 + commandLineMode(args[0], args[1]); + } else if (args.length == 1) { + // 一个参数:视为文件名,进行批量转换 + batchFileMode(args[0]); + } else { + System.out.println("用法:"); + System.out.println(" 交互模式: java TemperatureConverter"); + System.out.println(" 单次转换: java TemperatureConverter <数值> <单位(C/F)>"); + System.out.println(" 批量转换: java TemperatureConverter <文件名>"); + } + } } \ No newline at end of file diff --git a/capture_20260308204212948.jpg b/capture_20260308204212948.jpg new file mode 100644 index 0000000..82c78f4 Binary files /dev/null and b/capture_20260308204212948.jpg differ diff --git a/capture_20260308220314857.jpg b/capture_20260308220314857.jpg new file mode 100644 index 0000000..a4c7649 Binary files /dev/null and b/capture_20260308220314857.jpg differ