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.

153 lines
6.1 KiB

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
/**
* 温度转换工具
* 提供摄氏度和华氏度的相互转换功能,支持交互式命令行、命令行参数以及文件批量转换。
*/
class TemperatureConverter {
/**
* 将摄氏度转换为华氏度
* @param celsius 摄氏温度值
* @return 对应的华氏温度
*/
public static double celsiusToFahrenheit(double celsius) {
return celsius * 9.0 / 5.0 + 32;
}
/**
* 将华氏度转换为摄氏度
* @param fahrenheit 华氏温度值
* @return 对应的摄氏温度
*/
public static double fahrenheitToCelsius(double fahrenheit) {
return (fahrenheit - 32) * 5.0 / 9.0;
}
/**
* 主程序入口
* 支持三种运行模式:
* 1. 无参数:交互式菜单选择转换方向
* 2. 两个参数:java TemperatureConverter <温度值> <刻度标识> 刻度标识为 C 或 F(大小写不敏感)
* 3. 三个参数:java TemperatureConverter <温度值> <刻度标识> <文件名> 从文件读取多行进行批量转换
* @param args 命令行参数
*/
static void main(String[] args) {
if (args.length == 0) {
// 交互模式
runInteractive();
} else if (args.length >= 2) {
// 命令行参数模式
try {
double value = Double.parseDouble(args[0]);
String scale = args[1].toUpperCase();
if (scale.equals("C")) {
double result = celsiusToFahrenheit(value);
System.out.printf("%.2f°C = %.2f°F%n", value, result);
} else if (scale.equals("F")) {
double result = fahrenheitToCelsius(value);
System.out.printf("%.2f°F = %.2f°C%n", value, result);
} else {
System.err.println("错误:刻度标识必须是 C 或 F");
System.exit(1);
}
// 如果有第三个参数,表示文件批量转换
if (args.length >= 3) {
String filename = args[2];
batchConvertFromFile(filename);
}
} catch (NumberFormatException e) {
System.err.println("错误:温度值必须是有效的数字");
System.exit(1);
}
} else {
System.err.println("用法:");
System.err.println(" 交互模式:java TemperatureConverter");
System.err.println(" 单次转换:java TemperatureConverter <温度值> <刻度标识(C/F)>");
System.err.println(" 文件批量转换:java TemperatureConverter <温度值> <刻度标识(C/F)> <文件名>");
System.exit(1);
}
}
/**
* 交互式菜单模式
*/
private static void runInteractive() {
Scanner scanner = new Scanner(System.in);
System.out.println("温度转换器");
System.out.println("选择转换方向:");
System.out.println("1. 摄氏度 -> 华氏度");
System.out.println("2. 华氏度 -> 摄氏度");
System.out.print("请输入选择 (1 或 2): ");
String choice = scanner.nextLine();
if (choice.equals("1")) {
System.out.print("请输入摄氏度: ");
try {
double celsius = Double.parseDouble(scanner.nextLine());
double result = celsiusToFahrenheit(celsius);
System.out.printf("%.2f°C = %.2f°F%n", celsius, result);
} catch (NumberFormatException e) {
System.err.println("输入错误:请输入有效的数字");
}
} else if (choice.equals("2")) {
System.out.print("请输入华氏度: ");
try {
double fahrenheit = Double.parseDouble(scanner.nextLine());
double result = fahrenheitToCelsius(fahrenheit);
System.out.printf("%.2f°F = %.2f°C%n", fahrenheit, result);
} catch (NumberFormatException e) {
System.err.println("输入错误:请输入有效的数字");
}
} else {
System.err.println("无效选择");
}
scanner.close();
}
/**
* 从文件批量转换温度
* 文件每行格式:<温度值> <刻度标识> 例如:36.6 C 或 98.6 F
* 忽略空行和以#开头的注释行
* @param filename 文件名
*/
private static void batchConvertFromFile(String filename) {
System.out.println("\n批量转换结果:");
try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
String line;
int lineNum = 0;
while ((line = reader.readLine()) != null) {
lineNum++;
line = line.trim();
if (line.isEmpty() || line.startsWith("#")) {
continue; // 跳过空行和注释行
}
String[] parts = line.split("\\s+");
if (parts.length < 2) {
System.err.printf("第%d行格式错误,跳过:%s%n", lineNum, line);
continue;
}
try {
double value = Double.parseDouble(parts[0]);
String scale = parts[1].toUpperCase();
if (scale.equals("C")) {
double result = celsiusToFahrenheit(value);
System.out.printf("%.2f°C = %.2f°F%n", value, result);
} else if (scale.equals("F")) {
double result = fahrenheitToCelsius(value);
System.out.printf("%.2f°F = %.2f°C%n", value, result);
} else {
}
} catch (NumberFormatException e) {
System.err.printf("第%d行温度值无效,跳过:%s%n", lineNum, line);
}
}
} catch (IOException e) {
System.err.println("读取文件失败:" + e.getMessage());
}
}
}