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.
164 lines
4.9 KiB
164 lines
4.9 KiB
/**
|
|
* 将摄氏度转换为华氏度
|
|
*/
|
|
public static double celsiusToFahrenheit(double celsius) {
|
|
return celsius * 9.0 / 5.0 + 32.0;
|
|
}
|
|
|
|
/**
|
|
* 将华氏度转换为摄氏度
|
|
*/
|
|
public static double fahrenheitToCelsius(double fahrenheit) {
|
|
return (fahrenheit - 32.0) * 5.0 / 9.0;
|
|
}
|
|
|
|
/**
|
|
* 转换单个温度值
|
|
*/
|
|
public static String convertSingle(String valueStr, String unit) {
|
|
try {
|
|
double value = Double.parseDouble(valueStr);
|
|
unit = unit.toUpperCase();
|
|
|
|
if (unit.startsWith("C")) {
|
|
double result = celsiusToFahrenheit(value);
|
|
return String.format("%.2f °C = %.2f °F", value, result);
|
|
} else if (unit.startsWith("F")) {
|
|
double result = fahrenheitToCelsius(value);
|
|
return String.format("%.2f °F = %.2f °C", value, result);
|
|
} else {
|
|
return "错误:未知单位 '" + unit + "',请使用 C 或 F";
|
|
}
|
|
} catch (NumberFormatException e) {
|
|
return "错误:无效的温度值 '" + valueStr + "'";
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 解析并转换一行输入
|
|
*/
|
|
public static String parseAndConvert(String line) {
|
|
line = line.trim();
|
|
if (line.isEmpty() || line.startsWith("#")) {
|
|
return null; // 跳过空行和注释行
|
|
}
|
|
|
|
String[] parts = line.split("\\s+");
|
|
if (parts.length < 1) {
|
|
return null;
|
|
}
|
|
|
|
String valueStr = parts[0];
|
|
String unit = (parts.length > 1) ? parts[1] : "C";
|
|
|
|
return convertSingle(valueStr, unit);
|
|
}
|
|
|
|
/**
|
|
* 从文件批量转换温度
|
|
*/
|
|
public static void convertFromFile(String filename) {
|
|
IO.println("从文件读取温度进行批量转换: " + filename);
|
|
IO.println("----------------------------------------");
|
|
|
|
try {
|
|
List<String> lines = Files.readAllLines(Paths.get(filename));
|
|
int lineNumber = 0;
|
|
int convertedCount = 0;
|
|
|
|
for (String line : lines) {
|
|
lineNumber++;
|
|
String result = parseAndConvert(line);
|
|
|
|
if (result != null) {
|
|
IO.println("第" + lineNumber + "行: " + result);
|
|
convertedCount++;
|
|
}
|
|
}
|
|
|
|
IO.println("----------------------------------------");
|
|
IO.println("批量转换完成,共转换 " + convertedCount + " 条温度数据");
|
|
|
|
} catch (FileNotFoundException e) {
|
|
System.err.println("错误:找不到文件 '" + filename + "'");
|
|
} catch (IOException e) {
|
|
System.err.println("错误:读取文件时发生IO异常: " + e.getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 显示使用说明
|
|
*/
|
|
public static void showUsage() {
|
|
IO.println("温度转换器使用说明:");
|
|
IO.println("----------------------------------------");
|
|
IO.println("1. 交互式模式:");
|
|
IO.println(" java TemperatureConverter");
|
|
IO.println(" (然后根据提示输入温度)");
|
|
IO.println();
|
|
IO.println("2. 命令行参数模式:");
|
|
IO.println(" java TemperatureConverter <温度值> <单位>");
|
|
IO.println(" 例如: java TemperatureConverter 36.6 C");
|
|
IO.println(" java TemperatureConverter 97 F");
|
|
IO.println();
|
|
IO.println("3. 批量转换模式:");
|
|
IO.println(" java TemperatureConverter <文件名>");
|
|
IO.println(" 例如: java TemperatureConverter temperatures.txt");
|
|
IO.println();
|
|
IO.println("文件格式说明:");
|
|
IO.println("- 每行一个温度,格式: <数值> <单位>");
|
|
IO.println("- 单位可选: C(摄氏) 或 F(华氏),默认为C");
|
|
IO.println("- 空行和以#开头的行将被忽略");
|
|
IO.println("----------------------------------------");
|
|
}
|
|
|
|
void main(String[] args) {
|
|
// 检查是否有命令行参数
|
|
if (args.length == 0) {
|
|
// 交互式模式
|
|
interactiveMode();
|
|
} else if (args.length == 1) {
|
|
// 批量转换模式(从文件读取)
|
|
convertFromFile(args[0]);
|
|
} else {
|
|
// 命令行参数模式
|
|
String valueStr = args[0];
|
|
String unit = args[1];
|
|
String result = convertSingle(valueStr, unit);
|
|
IO.println(result);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 交互式模式
|
|
*/
|
|
public static void interactiveMode() {
|
|
Scanner scanner = new Scanner(System.in);
|
|
|
|
IO.println("欢迎使用温度转换器!");
|
|
showUsage();
|
|
|
|
while (true) {
|
|
IO.print("\n请输入要转换的温度与单位(例如 36.6 C 或 97 F,输入q退出):");
|
|
String input = scanner.nextLine().trim();
|
|
|
|
// 检查是否退出
|
|
if (input.equalsIgnoreCase("q") || input.equalsIgnoreCase("quit") ||
|
|
input.equalsIgnoreCase("exit")) {
|
|
IO.println("感谢使用,再见!");
|
|
break;
|
|
}
|
|
|
|
// 检查是否为空
|
|
if (input.isEmpty()) {
|
|
IO.println("输入为空,请重新输入。");
|
|
continue;
|
|
}
|
|
|
|
// 解析并转换
|
|
String result = parseAndConvert(input);
|
|
IO.println(Objects.requireNonNullElse(result, "输入格式错误,请按示例输入。"));
|
|
}
|
|
|
|
scanner.close();
|
|
}
|