commit 050593510e2327790e95e38b98d3465a48d7f7d5 Author: Bilei <3354484301@qq.com> Date: Sun Mar 8 21:37:00 2026 +0800 提交w1温度转换作业 diff --git a/w1/AI协助记录.docx b/w1/AI协助记录.docx new file mode 100644 index 0000000..1942dad Binary files /dev/null and b/w1/AI协助记录.docx differ diff --git a/w1/README.md b/w1/README.md new file mode 100644 index 0000000..4811ab9 --- /dev/null +++ b/w1/README.md @@ -0,0 +1,6 @@ +# TemperatureConverter +温度转换 Java 程序,实现摄氏度 ↔ 华氏度互转 +## 编译 +javac TemperatureConverter.java +## 运行 +java TemperatureConverter \ No newline at end of file diff --git a/w1/TemperatureConverter.java b/w1/TemperatureConverter.java new file mode 100644 index 0000000..b8d6c11 --- /dev/null +++ b/w1/TemperatureConverter.java @@ -0,0 +1,49 @@ +import java.util.Scanner; +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; + } + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + // 提示用户输入,格式示例:"36.6 C" 或 "97 F" + System.out.print("请输入要转换的温度与单位(例如 36.6 C 或 97 F):"); + String input = scanner.nextLine().trim(); + if (input.isEmpty()) { + System.out.println("输入为空,程序退出。"); + return; + } + String[] parts = input.split("\\s+"); // 按任意空白字符分割 + try { + 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("%.1f °C = %.2f °F%n", value, f); + } else if (unit.startsWith("F")) { + // 从华氏度转换为摄氏度 + double c = fahrenheitToCelsius(value); + System.out.printf("%.1f °F = %.2f °C%n", value, c); + } else { + System.out.println("未知单位,请使用 C 或 F。"); + } + } catch (Exception e) { + System.out.println("输入解析失败,请按示例输入数值与单位,例如 \"36.6 C\""); + } finally { + scanner.close(); + } + } +} \ No newline at end of file diff --git a/w1/desktop.ini b/w1/desktop.ini new file mode 100644 index 0000000..d6f335e --- /dev/null +++ b/w1/desktop.ini @@ -0,0 +1,2 @@ +[LocalizedFileNames] +Ļͼ 2026-03-08 211456.png=@Ļͼ 2026-03-08 211456,0 diff --git a/w1/运行截图.png b/w1/运行截图.png new file mode 100644 index 0000000..d84e9a2 Binary files /dev/null and b/w1/运行截图.png differ