Compare commits

...

3 Commits

  1. 5
      HelloWorld/HelloWorld.java
  2. 5
      helloWorld/HelloWorld.java
  3. 67
      w1 孙文轩 202506050229/TemperatureConverter.java
  4. 22
      w1 孙文轩 202506050229/readme.txt
  5. BIN
      w1 孙文轩 202506050229/运行截图1.png

5
HelloWorld/HelloWorld.java

@ -1,5 +0,0 @@
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}

5
helloWorld/HelloWorld.java

@ -1,5 +0,0 @@
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}

67
w1 孙文轩 202506050229/TemperatureConverter.java

@ -1,67 +0,0 @@
import java.util.Scanner;
/**
* TemperatureConverter
* 支持摄氏度(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;
}
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("输入为空,程序退出。");
scanner.close();
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("%.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 (NumberFormatException e) {
System.out.println("输入解析失败,请按示例输入数值与单位,例如:36.6 C");
} catch (Exception e) {
System.out.println("发生错误:" + e.getMessage());
} finally {
scanner.close();
}
}
}

22
w1 孙文轩 202506050229/readme.txt

@ -1,22 +0,0 @@
作业说明
这是一个温度转换的Java程序。
使用方法
1. 打开终端
2. 进入到文件所在的文件夹
3. 输入命令进行编译:
javac TemperatureConverter.java
4. 再输入命令运行:
java TemperatureConverter
5. 按照提示输入温度和单位即可
运行示例
请输入要转换的温度与单位(例如 36.6 C 或 97 F): 36.6 C
36.6 °C = 97.88 °F

BIN
w1 孙文轩 202506050229/运行截图1.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 376 KiB

Loading…
Cancel
Save