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.1 KiB

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
/**
* 温度转换器 - Java 版
* 对应 Python 程序功能,支持摄氏度与华氏度互转。
* 扩展功能:命令行参数单次转换、文件批量转换。
*/
public class TemperatureConverter {
/**
* 将摄氏度转换为华氏度
* @param celsius 摄氏温度
* @return 华氏温度
*/
public static double celsiusToFahrenheit(double celsius) {
return celsius * 9.0 / 5.0 + 32.0;
}
/**
* 将华氏度转换为摄氏度
* @param fahrenheit 华氏温度
* @return 摄氏温度
*/
public static double fahrenheitToCelsius(double fahrenheit) {
return (fahrenheit - 32.0) * 5.0 / 9.0;
}
/**
* 根据单位打印转换结果
* @param value 温度值
* @param unit 单位标识('C' 或 'F')
*/
private static void printConversion(double value, char unit) {
if (unit == 'C') {
double result = celsiusToFahrenheit(value);
System.out.printf("%.2f °C = %.2f °F%n", value, result);
} else if (unit == 'F') {
double result = fahrenheitToCelsius(value);
System.out.printf("%.2f °F = %.2f °C%n", value, result);
} else {
System.out.println("未知单位,请使用 C 或 F。");
}
}
/**
* 交互模式(与原 Python 程序行为完全一致)
* 提示用户输入一行,解析温度与单位(单位默认为 C)
*/
private static void interactiveMode() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入要转换的温度与单位(例如 36.6 C 或 97 F):");
String input = scanner.nextLine().trim();
if (input.isEmpty()) {
System.out.println("输入为空,程序退出。");
scanner.close();
return;
}
String[] parts = input.split("\\s+");
double value;
String unitStr = "C"; // 默认单位
try {
value = Double.parseDouble(parts[0]);
if (parts.length > 1) {
unitStr = parts[1];
}
} catch (NumberFormatException e) {
System.out.println("输入解析失败,请按示例输入数值与单位,例如:36.6 C");
scanner.close();
return;
}
char unit = unitStr.toUpperCase().charAt(0);
printConversion(value, unit);
scanner.close();
}
/**
* 命令行模式:支持单次转换和文件批量转换
* @param args 命令行参数
*/
private static void commandLineMode(String[] args) {
if (args.length == 2) {
// 单次转换: java TemperatureConverter 36.6 C
try {
double value = Double.parseDouble(args[0]);
char unit = args[1].toUpperCase().charAt(0);
printConversion(value, unit);
} catch (NumberFormatException e) {
System.out.println("温度值必须是数字。");
}
} else if (args.length == 1) {
// 批量转换: java TemperatureConverter input.txt
String fileName = args[0];
try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
String line;
while ((line = br.readLine()) != null) {
line = line.trim();
if (line.isEmpty()) continue;
String[] parts = line.split("\\s+");
if (parts.length >= 1) {
try {
double val = Double.parseDouble(parts[0]);
char unit = 'C'; // 默认单位
if (parts.length >= 2) {
unit = parts[1].toUpperCase().charAt(0);
}
printConversion(val, unit);
} catch (NumberFormatException e) {
System.out.println("文件行格式错误: " + line + " (温度不是数字)");
}
} else {
System.out.println("文件行格式错误: " + line);
}
}
} catch (IOException e) {
System.out.println("读取文件失败: " + e.getMessage());
}
} else {
System.out.println("用法:");
System.out.println(" 交互模式: java TemperatureConverter");
System.out.println(" 单次转换: java TemperatureConverter <温度值> <单位(C/F)>");
System.out.println(" 批量转换: java TemperatureConverter <文件名>");
}
}
public static void main(String[] args) {
if (args.length == 0) {
interactiveMode(); // 无参数时进入交互模式
} else {
commandLineMode(args);
}
}
}