Browse Source

上传文件至 ''

main
YuWeixia 1 month ago
parent
commit
f8f3d25f1c
  1. BIN
      AI 协助记录.docx
  2. BIN
      HelloWorld.java
  3. 9
      README.md
  4. 137
      TemperatureConverter.java
  5. BIN
      运行截图.png

BIN
AI 协助记录.docx

Binary file not shown.

BIN
HelloWorld.java

Binary file not shown.

9
README.md

@ -1,2 +1,9 @@
# java # 温度转换程序(Java版)
将 Python 温度转换程序移植为 Java,保留原功能并扩展了命令行参数与文件批量转换。
## 编译与运行
### 编译
```bash
javac TemperatureConverter.java

137
TemperatureConverter.java

@ -0,0 +1,137 @@
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
/**
* 温度转换器 - Java
* 对应 Python 程序功能支持摄氏度与华氏度互转
* 扩展功能命令行参数单次转换文件批量转换
*/
public class TemperatureConverter {
/**
* 将摄氏度转换为华氏度
* @param celsius 摄氏温度
* @return 华氏温度
*/
public static double celsiusToFahrenheit(double celsius) {
return celsius * 9.0 / 5.0 + 32.0;
}
/**
* 将华氏度转换为摄氏度
* @param fahrenheit 华氏温度
* @return 摄氏温度
*/
public static double fahrenheitToCelsius(double fahrenheit) {
return (fahrenheit - 32.0) * 5.0 / 9.0;
}
/**
* 根据单位打印转换结果
* @param value 温度值
* @param unit 单位标识'C' 'F'
*/
private static void printConversion(double value, char unit) {
if (unit == 'C') {
double result = celsiusToFahrenheit(value);
System.out.printf("%.2f °C = %.2f °F%n", value, result);
} else if (unit == 'F') {
double result = fahrenheitToCelsius(value);
System.out.printf("%.2f °F = %.2f °C%n", value, result);
} else {
System.out.println("未知单位,请使用 C 或 F。");
}
}
/**
* 交互模式与原 Python 程序行为完全一致
* 提示用户输入一行解析温度与单位单位默认为 C
*/
private static void interactiveMode() {
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("输入为空,程序退出。");
scanner.close();
return;
}
String[] parts = input.split("\\s+");
double value;
String unitStr = "C"; // 默认单位
try {
value = Double.parseDouble(parts[0]);
if (parts.length > 1) {
unitStr = parts[1];
}
} catch (NumberFormatException e) {
System.out.println("输入解析失败,请按示例输入数值与单位,例如:36.6 C");
scanner.close();
return;
}
char unit = unitStr.toUpperCase().charAt(0);
printConversion(value, unit);
scanner.close();
}
/**
* 命令行模式支持单次转换和文件批量转换
* @param args 命令行参数
*/
private static void commandLineMode(String[] args) {
if (args.length == 2) {
// 单次转换: java TemperatureConverter 36.6 C
try {
double value = Double.parseDouble(args[0]);
char unit = args[1].toUpperCase().charAt(0);
printConversion(value, unit);
} catch (NumberFormatException e) {
System.out.println("温度值必须是数字。");
}
} else if (args.length == 1) {
// 批量转换: java TemperatureConverter input.txt
String fileName = args[0];
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 >= 1) {
try {
double val = Double.parseDouble(parts[0]);
char unit = 'C'; // 默认单位
if (parts.length >= 2) {
unit = parts[1].toUpperCase().charAt(0);
}
printConversion(val, unit);
} catch (NumberFormatException e) {
System.out.println("文件行格式错误: " + line + " (温度不是数字)");
}
} else {
System.out.println("文件行格式错误: " + line);
}
}
} catch (IOException e) {
System.out.println("读取文件失败: " + e.getMessage());
}
} else {
System.out.println("用法:");
System.out.println(" 交互模式: java TemperatureConverter");
System.out.println(" 单次转换: java TemperatureConverter <温度值> <单位(C/F)>");
System.out.println(" 批量转换: java TemperatureConverter <文件名>");
}
}
public static void main(String[] args) {
if (args.length == 0) {
interactiveMode(); // 无参数时进入交互模式
} else {
commandLineMode(args);
}
}
}

BIN
运行截图.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 376 KiB

Loading…
Cancel
Save