You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

137 lines
5.3 KiB

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
/**
* 温度转换工具类
* 支持功能:
* 1. 摄氏度 ↔ 华氏度 互转
* 2. 命令行参数模式(如:java TemperatureConverter 36.6 C)
* 3. 批量文件转换(读取文件中多行温度数据并输出转换结果)
* 4. 交互式输入模式(原 Python 程序的核心交互逻辑)
*/
public class TemperatureConverter {
/**
* 将摄氏度转换为华氏度
* @param c 摄氏温度(double 类型,保证精度)
* @return double: 对应的华氏温度
*/
public static double celsiusToFahrenheit(double c) {
return c * 9.0 / 5.0 + 32.0;
}
/**
* 将华氏度转换为摄氏度
* @param f 华氏温度(double 类型,保证精度)
* @return double: 对应的摄氏温度
*/
public static double fahrenheitToCelsius(double f) {
return (f - 32.0) * 5.0 / 9.0;
}
/**
* 处理单个温度值的转换逻辑(核心复用逻辑)
* @param value 温度数值
* @param unit 温度单位(C/F,不区分大小写)
*/
public static void convertSingleTemperature(double value, String unit) {
String upperUnit = unit.toUpperCase();
if (upperUnit.startsWith("C")) {
// 摄氏度转华氏度
double f = celsiusToFahrenheit(value);
System.out.printf("%.2f °C = %.2f °F%n", value, f);
} else if (upperUnit.startsWith("F")) {
// 华氏度转摄氏度
double c = fahrenheitToCelsius(value);
System.out.printf("%.2f °F = %.2f °C%n", value, c);
} else {
System.out.println("未知单位,请使用 C 或 F。");
}
}
/**
* 批量处理文件中的温度转换
* @param filePath 文件路径(文件中每行格式:数值 单位,如 36.6 C)
*/
public static void batchConvertFromFile(String filePath) {
try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
String line;
System.out.println("===== 批量转换结果 =====");
while ((line = br.readLine()) != null) {
String trimmedLine = line.trim();
if (trimmedLine.isEmpty()) {
continue; // 跳过空行
}
String[] parts = trimmedLine.split("\\s+"); // 按任意空白符分割
try {
double value = Double.parseDouble(parts[0]);
String unit = parts.length > 1 ? parts[1] : "C";
convertSingleTemperature(value, unit);
} catch (Exception e) {
System.out.printf("行 [%s] 解析失败:%s%n", line, e.getMessage());
}
}
} catch (IOException e) {
System.out.println("文件读取失败:" + e.getMessage());
}
}
/**
* 交互式输入模式(原 Python 程序的 main 逻辑)
*/
public static void interactiveMode() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入要转换的温度与单位(例如 36.6 C 或 97 F):");
String s = scanner.nextLine().trim();
scanner.close();
if (s.isEmpty()) {
System.out.println("输入为空,程序退出。");
return;
}
String[] parts = s.split("\\s+");
try {
double value = Double.parseDouble(parts[0]);
String unit = parts.length > 1 ? parts[1] : "C";
convertSingleTemperature(value, unit);
} catch (Exception e) {
System.out.println("输入解析失败,请按示例输入数值与单位,例如:36.6 C");
}
}
/**
* 程序入口方法
* @param args 命令行参数:
* - 无参数:进入交互式模式
* - 2个参数:数值 单位(如 36.6 C)→ 单次转换
* - 1个参数:文件路径 → 批量转换
*/
public static void main(String[] args) {
if (args.length == 0) {
// 无参数 → 交互式模式
interactiveMode();
} else if (args.length == 1) {
// 1个参数 → 批量文件转换
batchConvertFromFile(args[0]);
} else if (args.length == 2) {
// 2个参数 → 命令行单次转换
try {
double value = Double.parseDouble(args[0]);
String unit = args[1];
convertSingleTemperature(value, unit);
} catch (Exception e) {
System.out.println("命令行参数解析失败,请按格式输入:java TemperatureConverter 36.6 C");
}
} else {
// 参数数量错误
System.out.println("参数数量错误!");
System.out.println("使用说明:");
System.out.println("1. 交互式模式:java TemperatureConverter");
System.out.println("2. 命令行单次转换:java TemperatureConverter 36.6 C");
System.out.println("3. 批量文件转换:java TemperatureConverter temp.txt");
}
}
}