1 changed files with 135 additions and 0 deletions
@ -0,0 +1,135 @@ |
|||
import java.util.Scanner; |
|||
import java.io.*; |
|||
import java.nio.file.*; |
|||
public class TemperatureConverter { |
|||
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; |
|||
} |
|||
|
|||
/** |
|||
* 解析并转换单行输入 (格式: "数值 单位" 或 "数值单位" 如 "36.6C") |
|||
* |
|||
* @param line 输入字符串 |
|||
* @return 转换结果字符串,若解析失败返回错误信息 |
|||
*/ |
|||
public static String convertLine(String line) { |
|||
line = line.trim(); |
|||
if (line.isEmpty()) { |
|||
return "输入为空,跳过。"; |
|||
} |
|||
|
|||
// 分离数值和单位部分(支持空格分隔或无空格)
|
|||
String[] parts = line.split("\\s+"); |
|||
String valueStr; |
|||
String unit; |
|||
|
|||
if (parts.length >= 2) { |
|||
valueStr = parts[0]; |
|||
unit = parts[1].toUpperCase(); |
|||
} else { |
|||
// 尝试从字符串尾部解析单位(如 "36.6C")
|
|||
String trimmed = parts[0]; |
|||
int splitPos = -1; |
|||
for (int i = 0; i < trimmed.length(); i++) { |
|||
char ch = trimmed.charAt(i); |
|||
if (Character.isLetter(ch)) { |
|||
splitPos = i; |
|||
break; |
|||
} |
|||
} |
|||
if (splitPos > 0) { |
|||
valueStr = trimmed.substring(0, splitPos); |
|||
unit = trimmed.substring(splitPos).toUpperCase(); |
|||
} else { |
|||
return "输入格式错误,无法解析单位和数值。"; |
|||
} |
|||
} |
|||
|
|||
// 转换数值部分
|
|||
double value; |
|||
try { |
|||
value = Double.parseDouble(valueStr); |
|||
} catch (NumberFormatException e) { |
|||
return "数值格式错误: " + valueStr; |
|||
} |
|||
|
|||
// 根据单位进行转换
|
|||
if (unit.startsWith("C")) { |
|||
double f = celsiusToFahrenheit(value); |
|||
return String.format("%.2f °C = %.2f °F", value, f); |
|||
} else if (unit.startsWith("F")) { |
|||
double c = fahrenheitToCelsius(value); |
|||
return String.format("%.2f °F = %.2f °C", value, c); |
|||
} else { |
|||
return "未知单位,请使用 C 或 F。"; |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* 交互式模式(对应原 Python main 函数) |
|||
*/ |
|||
public static void interactiveMode() { |
|||
try (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("输入为空,程序退出。"); |
|||
return; |
|||
} |
|||
String result = convertLine(input); |
|||
System.out.println(result); |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* 命令行参数模式:支持单次转换或批量文件处理 |
|||
* @param args 命令行参数 |
|||
*/ |
|||
public static void main(String[] args) { |
|||
if (args.length == 0) { |
|||
// 无参数:交互模式
|
|||
interactiveMode(); |
|||
} else if (args.length == 2) { |
|||
// 两个参数:数值 单位 (如 "36.6" "C")
|
|||
String input = args[0] + " " + args[1]; |
|||
String result = convertLine(input); |
|||
System.out.println(result); |
|||
} else if (args.length == 1) { |
|||
// 一个参数:可能是文件名,也可能是直接输入如 "36.6C"
|
|||
String arg = args[0]; |
|||
File file = new File(arg); |
|||
if (file.exists() && file.isFile()) { |
|||
// 批量文件转换模式
|
|||
try { |
|||
System.out.println("批量转换文件: " + arg); |
|||
Files.lines(Paths.get(arg)).forEach(line -> { |
|||
String result = convertLine(line); |
|||
System.out.println(result); |
|||
}); |
|||
} catch (IOException e) { |
|||
System.err.println("读取文件失败: " + e.getMessage()); |
|||
} |
|||
} else { |
|||
// 视为单行输入(无空格,如 "36.6C")
|
|||
String result = convertLine(arg); |
|||
System.out.println(result); |
|||
} |
|||
} else { |
|||
System.out.println("用法:"); |
|||
System.out.println(" 交互模式: java TemperatureConverter"); |
|||
System.out.println(" 单次转换: java TemperatureConverter <数值> <单位>"); |
|||
System.out.println(" 批量转换: java TemperatureConverter <文件名>"); |
|||
System.out.println(" 紧凑输入: java TemperatureConverter <数值及单位> (例如 36.6C)"); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue