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.
145 lines
4.6 KiB
145 lines
4.6 KiB
//温度转换器示例程序(java)
|
|
//支持摄氏度(C)与华氏度(F)之间互转
|
|
|
|
//功能:
|
|
// 1. 交互模式 (默认)
|
|
// 2. 命令行参数模式 (java TemperatureConverter 36.6 C)
|
|
// 3. 文件批量模式 (java TemperatureConverterdata.txt)
|
|
|
|
import java.util.Scanner;
|
|
import java.io.File;
|
|
import java.io.FileNotFoundException;
|
|
|
|
public class TemperatureConverter {
|
|
|
|
public static double celsius_to_fahrenheit (double c) {
|
|
/*将摄氏度转换为华氏度。
|
|
参数:c (float): 摄氏温度
|
|
返回:float: 对应的华氏温度*/
|
|
return c * 9.0 / 5.0 + 32.0;
|
|
}
|
|
|
|
|
|
public static double fahrenheit_to_celsius (double f) {
|
|
/*将华氏度转换为摄氏度。
|
|
参数:f (float): 华氏温度
|
|
返回:float: 对应的摄氏温度*/
|
|
return (f - 32.0) * 5.0 / 9.0;
|
|
}
|
|
|
|
|
|
public static void processLine(String s) {
|
|
s=s.trim();
|
|
|
|
if (s.isEmpty()){
|
|
System.out.println("输入为空,程序退出。");
|
|
return;
|
|
}
|
|
|
|
|
|
String[] parts=s.split("\\s+");
|
|
|
|
try{
|
|
if (parts.length < 2) {
|
|
System.out.println("输入解析失败,请按示例输入数值与单位,例如:36.6 C");
|
|
return; // 直接结束方法,防止后续代码执行
|
|
}
|
|
//允许用户输入两个部分:数值与单位
|
|
double value = Double.parseDouble(parts[0]);
|
|
String unit = parts[1].toUpperCase();
|
|
|
|
if (unit.equals("C")) {
|
|
//从摄氏度转换为华氏度
|
|
double f = celsius_to_fahrenheit(value);
|
|
System.out.println(value + " °C = "+ String.format("%.2f",f)+" °F");
|
|
} else if (unit.equals("F")) {
|
|
//从华氏度转换为摄氏度
|
|
double c = fahrenheit_to_celsius(value);
|
|
System.out.println(value + " °F = "+ String.format("%.2f",c)+" °C");
|
|
} else {
|
|
System.out.println("未知单位,请使用 C 或 F。");
|
|
}
|
|
|
|
} catch (NumberFormatException e) {
|
|
System.out.println("输入解析失败,请按示例输入数值与单位,例如:36.6 C");
|
|
}
|
|
}
|
|
public static void main(String[] args) {
|
|
|
|
// ================= 模式 1: 命令行参数模式 =================
|
|
// 用法: java TemperatureConverter 36.6 C
|
|
if (args.length == 2) {
|
|
String inputLine = args[0] + " " + args[1];
|
|
// 直接处理这一行,不进入循环
|
|
processLine(inputLine);
|
|
return; // 结束程序
|
|
}
|
|
|
|
// ================= 模式 2: 文件批量模式 =================
|
|
// 用法: java TemperatureConverter temps.txt
|
|
if (args.length == 1) {
|
|
String fileName = args[0];
|
|
File file = new File(fileName);
|
|
|
|
if (!file.exists()) {
|
|
System.err.println("错误: 找不到文件: " + fileName);
|
|
System.err.println("请确保文件在当前目录下,例如: java TemperatureConverter temps.txt");
|
|
return;
|
|
}
|
|
|
|
if (!file.isFile()) {
|
|
System.err.println("错误: " + fileName + " 不是一个有效文件。");
|
|
return;
|
|
}
|
|
|
|
System.out.println(">>> 开始从文件 [" + fileName + "] 批量读取...");
|
|
|
|
Scanner fileScanner = null;
|
|
try {
|
|
fileScanner = new Scanner(file);
|
|
int lineCount = 0;
|
|
while (fileScanner.hasNextLine()) {
|
|
String line = fileScanner.nextLine();
|
|
// 跳过空行,但不退出
|
|
if (line.trim().isEmpty()) continue;
|
|
|
|
processLine(line);
|
|
lineCount++;
|
|
}
|
|
System.out.println(">>> 处理完成,共读取 " + lineCount + " 行数据。");
|
|
|
|
} catch (FileNotFoundException e) {
|
|
// 理论上前面检查过 exists,但以防万一
|
|
System.err.println("系统错误: 无法读取文件。");
|
|
} finally {
|
|
if (fileScanner != null) {
|
|
fileScanner.close();
|
|
}
|
|
}
|
|
return; // 结束程序
|
|
}
|
|
|
|
// ================= 模式 3: 交互模式 (默认) =================
|
|
// 用法: java TemperatureConverter
|
|
Scanner scanner = new Scanner(System.in);
|
|
System.out.println("=== 温度转换器 (交互模式) ===");
|
|
// 提示用户输入,格式示例:"36.6 C" 或 "97 F"
|
|
System.out.println("请输入要转换的温度与单位(例如 36.6 C 或 97 F):");
|
|
System.out.println("输入 'quit' 退出程序。");
|
|
|
|
while (true) {
|
|
System.out.print("> ");
|
|
String s = scanner.nextLine(); // 保留变量名 s
|
|
|
|
if (s.trim().equalsIgnoreCase("quit") || s.trim().equalsIgnoreCase("exit")) {
|
|
System.out.println("程序已退出。");
|
|
break;
|
|
}
|
|
|
|
// 调用统一的处理方法
|
|
processLine(s);
|
|
}
|
|
|
|
scanner.close();
|
|
}
|
|
}
|