/** * 温度转换器程序(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(); } } }