5 changed files with 211 additions and 0 deletions
@ -0,0 +1,29 @@ |
|||
# Created by https://gitignore.org |
|||
# Java.gitignore |
|||
|
|||
.idea/ |
|||
|
|||
# Compiled class file |
|||
*.class |
|||
|
|||
# Log file |
|||
*.log |
|||
|
|||
# BlueJ files |
|||
*.ctxt |
|||
|
|||
# Mobile Tools for Java (J2ME) |
|||
.mtj.tmp/ |
|||
|
|||
# Package Files # |
|||
*.jar |
|||
*.war |
|||
*.nar |
|||
*.ear |
|||
*.zip |
|||
*.tar.gz |
|||
*.rar |
|||
|
|||
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml |
|||
hs_err_pid* |
|||
replay_pid* |
|||
@ -0,0 +1,2 @@ |
|||
TemperatureConverter.py |
|||
example.txt |
|||
@ -0,0 +1,29 @@ |
|||
# AI 使用记录 |
|||
|
|||
在此目录下使用 opencode 与 glm-4.6v-flash 模型。 |
|||
|
|||
## 格式说明 |
|||
|
|||
> (这是 opencode 所用模式) |
|||
> |
|||
> 这是用户输入 |
|||
|
|||
这是概括的 AI 输出。 |
|||
|
|||
## 对话内容 |
|||
|
|||
> (Plan) |
|||
> |
|||
> 将 TemperatureConverter.py 转换为同等的可运行的 java 文件。 |
|||
|
|||
给出了接下来要执行的步骤。 |
|||
|
|||
> (Build) |
|||
> |
|||
> You are in build mode now. Continue. |
|||
|
|||
正确输出了程序主体,但注释和 python 源文件不一致。 |
|||
|
|||
> 如何运行这个程序? |
|||
|
|||
给出了正确的编译和运行命令。 |
|||
@ -0,0 +1,48 @@ |
|||
# 温度转换器 |
|||
|
|||
## 编译 |
|||
|
|||
```console |
|||
$ javac TemperatureConverter.java |
|||
``` |
|||
|
|||
应输出 `TemperatureConverter.class` 文件。 |
|||
|
|||
## 使用说明 |
|||
|
|||
未传入参数时,程序将提示输入值。 |
|||
|
|||
请按**数字+空格+单位**(如 `36.6 C`)的格式输入,程序会将温度转换后输出。单位 `C` 代表摄氏度,`F` 代表华氏度。未输入单位时,程序将默认输入为摄氏度。 |
|||
|
|||
```console |
|||
$ java TemperatureConverter |
|||
请输入要转换的温度与单位(例如 36.6 C 或 97 F):36.6 |
|||
36.60 °C = 97.88 °F |
|||
``` |
|||
|
|||
当有参数传入时,程序**仅会**将**第一个**参数解析为温度输入,并将其转换后输出。 |
|||
|
|||
```console |
|||
$ java TemperatureConverter "36.6 F" "36.6 C" |
|||
36.60 °F = 2.56 °C |
|||
``` |
|||
|
|||
若参数为有效的文件路径,程序将解析指定文件中有内容的行,并将每行视为一个温度进行转换。例如以下文件内容: |
|||
|
|||
```plaintext |
|||
12 C |
|||
36 F |
|||
haha |
|||
|
|||
36.6 F |
|||
``` |
|||
|
|||
输出为: |
|||
|
|||
```console |
|||
$ java TemperatureConverter example.txt |
|||
12.00 °C = 53.60 °F |
|||
36.00 °F = 2.22 °C |
|||
输入 "haha" 解析失败,请按示例输入数值与单位,例如:36.6 C |
|||
36.60 °F = 2.56 °C |
|||
``` |
|||
@ -0,0 +1,103 @@ |
|||
import java.io.IOException; |
|||
import java.nio.file.Files; |
|||
import java.nio.file.Paths; |
|||
import java.util.stream.Stream; |
|||
|
|||
/** |
|||
* 温度转换器程序(Java) |
|||
* <br> |
|||
* 支持摄氏度(C)与华氏度(F)之间互转 |
|||
*/ |
|||
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; |
|||
} |
|||
|
|||
/** |
|||
* 将输入字符串解析为温度并通过 `println` 输出 |
|||
* |
|||
* @param input 形如 "36.6 C" 的输入 |
|||
*/ |
|||
public static void convertTemperatureInputAndOutput(String input) { |
|||
try { |
|||
String[] parts = input.trim().split("\\s+"); |
|||
|
|||
// 允许用户输入两个部分:数值与单位
|
|||
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("%.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。"); |
|||
} |
|||
} catch (Exception e) { |
|||
System.out.printf("输入 \"%s\" 解析失败,请按示例输入数值与单位,例如:36.6 C\n", input); |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* |
|||
*/ |
|||
public static void handleFile(String filename) throws IOException { |
|||
try (Stream<String> lines = Files.lines(Paths.get(filename))) { |
|||
lines.filter(it -> !it.trim().isEmpty()).forEach(TemperatureConverter::convertTemperatureInputAndOutput); |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* 程序入口 |
|||
* |
|||
* @param args 传入参数<br> |
|||
* 仅解析传入的第一个参数。首先尝试将其解析为文件路径,否则视为温度格式输入。<br> |
|||
* 没有参数传入时,提示用户输入 |
|||
*/ |
|||
public static void main(String[] args) { |
|||
if (args.length >= 1) { |
|||
String cliParam = args[0]; |
|||
|
|||
if (Files.isRegularFile(Paths.get(cliParam))) { |
|||
try { |
|||
handleFile(cliParam); |
|||
} catch (IOException e) { |
|||
System.out.printf("在处理文件 \"%s\" 时失败:%s", cliParam, e); |
|||
} |
|||
} else { |
|||
convertTemperatureInputAndOutput(cliParam); |
|||
} |
|||
|
|||
// 有参数传入时,不再提示用户输入
|
|||
return; |
|||
} |
|||
|
|||
// 提示用户输入,格式示例:"36.6 C" 或 "97 F"
|
|||
System.out.print("请输入要转换的温度与单位(例如 36.6 C 或 97 F):"); |
|||
String input = System.console().readLine(); |
|||
|
|||
if (input == null || input.trim().isEmpty()) { |
|||
System.out.println("输入为空,程序退出。"); |
|||
} else { |
|||
convertTemperatureInputAndOutput(input); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue