diff --git a/TemperatureConverter.java b/TemperatureConverter.java new file mode 100644 index 0000000..eae07a0 --- /dev/null +++ b/TemperatureConverter.java @@ -0,0 +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 <文件名>"); + } + } +} \ No newline at end of file