1 changed files with 0 additions and 109 deletions
@ -1,109 +0,0 @@ |
|||
import java.io.BufferedReader; |
|||
import java.io.FileReader; |
|||
import java.io.File; |
|||
import java.util.Scanner; |
|||
|
|||
/** |
|||
* 温度转换器:支持摄氏/华氏互转,含三种模式 |
|||
*/ |
|||
public class TemperatureConverter { |
|||
|
|||
/** |
|||
* 摄氏转华氏 |
|||
* @param c 摄氏温度 |
|||
* @return 华氏温度 |
|||
*/ |
|||
public static double celsiusToFahrenheit(double c) { |
|||
return c * 9.0 / 5.0 + 32.0; |
|||
} |
|||
|
|||
/** |
|||
* 华氏转摄氏 |
|||
* @param f 华氏温度 |
|||
* @return 摄氏温度 |
|||
*/ |
|||
public static double fahrenheitToCelsius(double f) { |
|||
return (f - 32.0) * 5.0 / 9.0; |
|||
} |
|||
|
|||
/** |
|||
* 从文件批量转换 |
|||
* @param filename 输入文件名 |
|||
*/ |
|||
public static void batchConvertFromFile(String filename) { |
|||
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 < 2) continue; |
|||
try { |
|||
double value = Double.parseDouble(parts[0]); |
|||
String unit = parts[1].toUpperCase(); |
|||
if (unit.startsWith("C")) { |
|||
double f = celsiusToFahrenheit(value); |
|||
System.out.printf("%.1f °C = %.2f °F%n", value, f); |
|||
} else if (unit.startsWith("F")) { |
|||
double c = fahrenheitToCelsius(value); |
|||
System.out.printf("%.1f °F = %.2f °C%n", value, c); |
|||
} |
|||
} catch (Exception e) { |
|||
System.out.println("跳过无效行: " + line); |
|||
} |
|||
} |
|||
} catch (Exception e) { |
|||
System.out.println("读取文件失败: " + e.getMessage()); |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* 主入口:支持三种模式 |
|||
* @param args 命令行参数 |
|||
*/ |
|||
public static void main(String[] args) { |
|||
// 模式三:批量文件转换
|
|||
if (args.length == 1 && new File(args[0]).exists()) { |
|||
batchConvertFromFile(args[0]); |
|||
return; |
|||
} |
|||
|
|||
Scanner scanner = new Scanner(System.in); |
|||
String s; |
|||
|
|||
// 模式二:命令行参数
|
|||
if (args.length > 0) { |
|||
s = String.join(" ", args); |
|||
} else { |
|||
// 模式一:交互式输入
|
|||
System.out.print("请输入温度与单位(如 36.6 C 或 97 F):"); |
|||
s = scanner.nextLine().trim(); |
|||
} |
|||
|
|||
if (s.isEmpty()) { |
|||
System.out.println("输入为空,程序退出。"); |
|||
scanner.close(); |
|||
return; |
|||
} |
|||
|
|||
String[] parts = s.split("\\s+"); |
|||
try { |
|||
double value = Double.parseDouble(parts[0]); |
|||
String unit = (parts.length > 1) ? parts[1].toUpperCase() : "C"; |
|||
|
|||
if (unit.startsWith("C")) { |
|||
double f = celsiusToFahrenheit(value); |
|||
System.out.printf("%.1f °C = %.2f °F%n", value, f); |
|||
} else if (unit.startsWith("F")) { |
|||
double c = fahrenheitToCelsius(value); |
|||
System.out.printf("%.1f °F = %.2f °C%n", value, c); |
|||
} else { |
|||
System.out.println("未知单位,请使用 C 或 F。"); |
|||
} |
|||
} catch (Exception e) { |
|||
System.out.println("输入解析失败,请按示例输入,如:36.6 C"); |
|||
} finally { |
|||
scanner.close(); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue