1 changed files with 169 additions and 0 deletions
@ -0,0 +1,169 @@ |
|||
import java.io.BufferedReader; |
|||
import java.io.FileReader; |
|||
import java.io.IOException; |
|||
import java.util.Scanner; |
|||
|
|||
/** |
|||
* 温度转换器(完整版) |
|||
* 功能:支持摄氏度(C)与华氏度(F)互转,提供三种使用模式: |
|||
* 1. 交互式输入模式(无参数) |
|||
* 2. 命令行单条转换模式(参数为数值+单位) |
|||
* 3. 批量文件转换模式(参数为 -f + 文件路径) |
|||
* 作者:xxx |
|||
* 日期:2026-03-10 |
|||
*/ |
|||
|
|||
// 关键修正:类声明添加 public,匹配文件名 |
|||
public class TemperatureConverter { |
|||
|
|||
/** |
|||
* 将摄氏度转换为华氏度 |
|||
* @param c 摄氏温度(浮点型),支持正负数值 |
|||
* @return double 对应的华氏温度,保留浮点精度 |
|||
*/ |
|||
public static double celsiusToFahrenheit(double c) { |
|||
return c * 9.0 / 5.0 + 32.0; |
|||
} |
|||
|
|||
/** |
|||
* 将华氏度转换为摄氏度 |
|||
* @param f 华氏温度(浮点型),支持正负数值 |
|||
* @return double 对应的摄氏温度,保留浮点精度 |
|||
*/ |
|||
public static double fahrenheitToCelsius(double f) { |
|||
return (f - 32.0) * 5.0 / 9.0; |
|||
} |
|||
|
|||
/** |
|||
* 核心转换逻辑:解析单个温度字符串并输出结果 |
|||
* |
|||
* @param input 温度字符串(格式:数值 单位,如 "36.6 C" 或 "97 F") |
|||
*/ |
|||
public static void convertTemperature(String input) { |
|||
String trimmedInput = input.trim(); |
|||
// 处理空输入 |
|||
if (trimmedInput.isEmpty()) { |
|||
String msg = "输入为空,跳过转换"; |
|||
System.out.println(msg); |
|||
return; |
|||
} |
|||
|
|||
// 分割输入(支持任意空白符:空格/制表符等) |
|||
String[] parts = trimmedInput.split("\\s+"); |
|||
try { |
|||
// 解析数值和单位 |
|||
double value = Double.parseDouble(parts[0]); |
|||
String unit = parts.length > 1 ? parts[1].toUpperCase() : "C"; |
|||
|
|||
// 摄氏度转华氏度 |
|||
if (unit.startsWith("C")) { |
|||
double fahrenheit = celsiusToFahrenheit(value); |
|||
String result = String.format("%.2f °C = %.2f °F", value, fahrenheit); |
|||
System.out.println(result); |
|||
} |
|||
// 华氏度转摄氏度 |
|||
else if (unit.startsWith("F")) { |
|||
double celsius = fahrenheitToCelsius(value); |
|||
String result = String.format("%.2f °F = %.2f °C", value, celsius); |
|||
System.out.println(result); |
|||
} |
|||
// 未知单位 |
|||
else { |
|||
String error = "未知单位,请使用 C 或 F:" + trimmedInput; |
|||
System.out.println(error); |
|||
} |
|||
} catch (NumberFormatException e) { |
|||
// 数值解析失败 |
|||
String error = "输入解析失败:" + trimmedInput + "(格式示例:36.6 C)"; |
|||
System.out.println(error); |
|||
} catch (Exception e) { |
|||
// 其他异常 |
|||
String error = "处理失败:" + trimmedInput + ",原因:" + e.getMessage(); |
|||
System.out.println(error); |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* 批量处理文件中的温度转换 |
|||
* @param filePath 输入文件路径(相对/绝对路径均可) |
|||
*/ |
|||
public static void processFile(String filePath) { |
|||
// 使用try-with-resources自动关闭文件流,避免资源泄漏 |
|||
try (BufferedReader br = new BufferedReader(new FileReader(filePath))) { |
|||
String line; |
|||
System.out.println("\n===== 批量文件转换开始 ====="); |
|||
int lineNum = 0; |
|||
while ((line = br.readLine()) != null) { |
|||
lineNum++; |
|||
System.out.printf("第 %d 行:", lineNum); |
|||
convertTemperature(line); |
|||
} |
|||
System.out.println("===== 批量文件转换完成 =====\n"); |
|||
} catch (IOException e) { |
|||
System.err.println("文件读取失败:" + e.getMessage()); |
|||
System.err.println("请检查文件路径是否正确,或文件是否存在"); |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* 交互式输入模式(无命令行参数时触发) |
|||
*/ |
|||
public static void interactiveMode() { |
|||
Scanner scanner = new Scanner(System.in); |
|||
System.out.println("\n===== 温度转换器 - 交互式模式 ====="); |
|||
System.out.print("请输入要转换的温度与单位(例如 36.6 C 或 97 F):"); |
|||
String input = scanner.nextLine(); |
|||
convertTemperature(input); |
|||
scanner.close(); |
|||
} |
|||
|
|||
/** |
|||
* 打印程序使用说明(参数错误时触发) |
|||
*/ |
|||
private static void printUsage() { |
|||
System.out.println("\n===== 温度转换器 使用说明 ====="); |
|||
System.out.println("1. 交互式模式(无参数):"); |
|||
System.out.println(" 命令:java TemperatureConverter"); |
|||
System.out.println(" 说明:按提示输入温度和单位即可转换"); |
|||
System.out.println("2. 命令行单条转换:"); |
|||
System.out.println(" 命令:java TemperatureConverter 36.6 C"); |
|||
System.out.println(" 说明:直接传入数值和单位,无需交互"); |
|||
System.out.println("3. 批量文件转换:"); |
|||
System.out.println(" 命令:java TemperatureConverter -f temperatures.txt"); |
|||
System.out.println(" 说明:读取文件中每行的温度并批量转换"); |
|||
System.out.println("==============================\n"); |
|||
} |
|||
|
|||
/** |
|||
* 程序入口方法 |
|||
* @param args 命令行参数 |
|||
*/ |
|||
public static void main(String[] args) { |
|||
// 优先处理命令行参数 |
|||
if (args.length > 0) { |
|||
// 批量文件转换模式 |
|||
if (args[0].equalsIgnoreCase("-f")) { |
|||
if (args.length >= 2) { |
|||
processFile(args[1]); |
|||
} else { |
|||
System.err.println("错误:-f 参数后必须指定文件路径!"); |
|||
printUsage(); |
|||
} |
|||
} |
|||
// 命令行单条转换模式 |
|||
else if (args.length >= 2) { |
|||
String input = args[0] + " " + args[1]; |
|||
convertTemperature(input); |
|||
} |
|||
// 参数格式错误 |
|||
else { |
|||
System.err.println("错误:命令行参数格式不正确!"); |
|||
printUsage(); |
|||
} |
|||
} |
|||
// 无参数:进入交互式模式 |
|||
else { |
|||
interactiveMode(); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue