TemperatureConverter.java #1

Open
Zhengjie wants to merge 10 commits from TemperatureConverter.java into main
  1. 4
      AI help record
  2. 4
      AI协助记录
  3. 10
      README.md
  4. 230
      TemperatureConverter.java
  5. BIN
      capture_20260308204212948.jpg
  6. BIN
      capture_20260308220314857.jpg
  7. BIN
      程序运行截图/capture_20260308204212948.jpg
  8. BIN
      程序运行截图/capture_20260308220314857.jpg

4
AI help record

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

4
AI协助记录

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

10
README.md

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

230
TemperatureConverter → TemperatureConverter.java

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

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