Browse Source

W\

TemperatureConverter.java
Zhengjie 1 month ago
parent
commit
4666bd9718
  1. 4
      AI协助记录
  2. 13
      README.md
  3. 230
      TemperatureConverter.java
  4. BIN
      capture_20260308204212948.jpg
  5. BIN
      capture_20260308220314857.jpg

4
AI协助记录

@ -0,0 +1,4 @@
AI 协助记录:
我使用了以下 prompt:
“请将下面的 Python 温度转换程序改写为等效的 Java 程序,保留注释并保证功能一致。同时支持命令行参数模式和批量文件读取转换。”
AI 给出了完整的 Java 代码,并解释了每个方法的作用。我根据 AI 的建议添加了三种运行模式并整理了 README 和运行示例,确保符合提交要求。

13
README.md

@ -1,7 +1,8 @@
# 温度转换器 # 温度转换器 (Java 版)
将 Python 温度转换程序移植为 Java,支持摄氏度与华氏度互转。
实现了三种运行模式:交互式、命令行参数、批量文件处理。 将 Python 温度转换程序移植为 Java,支持摄氏度与华氏度互转。
实现了三种运行模式:交互式、命令行参数、批量文件处理。
## 编译
```bash ## 编译
```bash
javac TemperatureConverter.java javac TemperatureConverter.java

230
TemperatureConverter.java

@ -1,116 +1,116 @@
import java.io.*; import java.io.*;
import java.util.*; import java.util.*;
public class TemperatureConverter { public class TemperatureConverter {
/** /**
* 将摄氏度转换为华氏度 * 将摄氏度转换为华氏度
* *
* @param c 摄氏温度 * @param c 摄氏温度
* @return 对应的华氏温度 * @return 对应的华氏温度
*/ */
public static double celsiusToFahrenheit(double c) { public static double celsiusToFahrenheit(double c) {
return c * 9.0 / 5.0 + 32.0; return c * 9.0 / 5.0 + 32.0;
} }
/** /**
* 将华氏度转换为摄氏度 * 将华氏度转换为摄氏度
* *
* @param f 华氏温度 * @param f 华氏温度
* @return 对应的摄氏温度 * @return 对应的摄氏温度
*/ */
public static double fahrenheitToCelsius(double f) { public static double fahrenheitToCelsius(double f) {
return (f - 32.0) * 5.0 / 9.0; return (f - 32.0) * 5.0 / 9.0;
} }
/** /**
* 交互式模式从标准输入读取一行解析并转换 * 交互式模式从标准输入读取一行解析并转换
*/ */
private static void interactiveMode() { private static void interactiveMode() {
Scanner scanner = new Scanner(System.in); Scanner scanner = new Scanner(System.in);
System.out.print("请输入要转换的温度与单位(例如 36.6 C 或 97 F):"); System.out.print("请输入要转换的温度与单位(例如 36.6 C 或 97 F):");
String line = scanner.nextLine().trim(); String line = scanner.nextLine().trim();
if (line.isEmpty()) { if (line.isEmpty()) {
System.out.println("输入为空,程序退出。"); System.out.println("输入为空,程序退出。");
return; return;
} }
processInputLine(line); processInputLine(line);
} }
/** /**
* 处理单行输入字符串格式如 "36.6 C" "97 F" * 处理单行输入字符串格式如 "36.6 C" "97 F"
* 若解析成功则输出转换结果否则打印错误信息 * 若解析成功则输出转换结果否则打印错误信息
*/ */
private static void processInputLine(String line) { private static void processInputLine(String line) {
String[] parts = line.split("\\s+"); String[] parts = line.split("\\s+");
if (parts.length == 0) return; if (parts.length == 0) return;
try { try {
double value = Double.parseDouble(parts[0]); double value = Double.parseDouble(parts[0]);
String unit = (parts.length > 1) ? parts[1].toUpperCase() : "C"; String unit = (parts.length > 1) ? parts[1].toUpperCase() : "C";
convertAndPrint(value, unit); convertAndPrint(value, unit);
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
System.out.println("输入解析失败,请按示例输入数值与单位,例如:36.6 C"); System.out.println("输入解析失败,请按示例输入数值与单位,例如:36.6 C");
} }
} }
/** /**
* 根据数值和单位进行转换并打印结果 * 根据数值和单位进行转换并打印结果
*/ */
private static void convertAndPrint(double value, String unit) { private static void convertAndPrint(double value, String unit) {
if (unit.startsWith("C")) { if (unit.startsWith("C")) {
double f = celsiusToFahrenheit(value); double f = celsiusToFahrenheit(value);
System.out.printf("%.2f °C = %.2f °F%n", value, f); System.out.printf("%.2f °C = %.2f °F%n", value, f);
} else if (unit.startsWith("F")) { } else if (unit.startsWith("F")) {
double c = fahrenheitToCelsius(value); double c = fahrenheitToCelsius(value);
System.out.printf("%.2f °F = %.2f °C%n", value, c); System.out.printf("%.2f °F = %.2f °C%n", value, c);
} else { } else {
System.out.println("未知单位,请使用 C 或 F。"); System.out.println("未知单位,请使用 C 或 F。");
} }
} }
/** /**
* 命令行参数模式两个参数分别为数值和单位 * 命令行参数模式两个参数分别为数值和单位
*/ */
private static void commandLineMode(String valueStr, String unitStr) { private static void commandLineMode(String valueStr, String unitStr) {
try { try {
double value = Double.parseDouble(valueStr); double value = Double.parseDouble(valueStr);
String unit = unitStr.toUpperCase(); String unit = unitStr.toUpperCase();
convertAndPrint(value, unit); convertAndPrint(value, unit);
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
System.out.println("无效的数值参数,请提供正确的数字。"); System.out.println("无效的数值参数,请提供正确的数字。");
} }
} }
/** /**
* 批量文件模式从指定文件逐行读取每行格式同交互输入 * 批量文件模式从指定文件逐行读取每行格式同交互输入
*/ */
private static void batchFileMode(String fileName) { private static void batchFileMode(String fileName) {
try (Scanner fileScanner = new Scanner(new File(fileName))) { try (Scanner fileScanner = new Scanner(new File(fileName))) {
while (fileScanner.hasNextLine()) { while (fileScanner.hasNextLine()) {
String line = fileScanner.nextLine().trim(); String line = fileScanner.nextLine().trim();
if (line.isEmpty()) continue; if (line.isEmpty()) continue;
processInputLine(line); processInputLine(line);
} }
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
System.out.println("文件未找到: " + fileName); System.out.println("文件未找到: " + fileName);
} }
} }
public static void main(String[] args) { public static void main(String[] args) {
if (args.length == 0) { if (args.length == 0) {
// 无参数:交互模式 // 无参数:交互模式
interactiveMode(); interactiveMode();
} else if (args.length == 2) { } else if (args.length == 2) {
// 两个参数:命令行模式 // 两个参数:命令行模式
commandLineMode(args[0], args[1]); commandLineMode(args[0], args[1]);
} else if (args.length == 1) { } else if (args.length == 1) {
// 一个参数:视为文件名,进行批量转换 // 一个参数:视为文件名,进行批量转换
batchFileMode(args[0]); batchFileMode(args[0]);
} else { } else {
System.out.println("用法:"); System.out.println("用法:");
System.out.println(" 交互模式: java TemperatureConverter"); System.out.println(" 交互模式: java TemperatureConverter");
System.out.println(" 单次转换: java TemperatureConverter <数值> <单位(C/F)>"); System.out.println(" 单次转换: java TemperatureConverter <数值> <单位(C/F)>");
System.out.println(" 批量转换: java TemperatureConverter <文件名>"); System.out.println(" 批量转换: java TemperatureConverter <文件名>");
} }
} }
} }

BIN
capture_20260308204212948.jpg

Binary file not shown.

After

Width:  |  Height:  |  Size: 286 KiB

BIN
capture_20260308220314857.jpg

Binary file not shown.

After

Width:  |  Height:  |  Size: 280 KiB

Loading…
Cancel
Save