Browse Source

删除 'TemperatureConverter.java'

main
Chengwuyi 1 month ago
parent
commit
c66939be17
  1. 67
      TemperatureConverter.java

67
TemperatureConverter.java

@ -1,67 +0,0 @@
/******************************************************************************
Online Java Compiler.
Code, Compile, Run and Debug java program online.
Write your code in this editor and press "Run" button to execute it.
*******************************************************************************/
import java.util.Scanner;
/**
* 温度转换器支持摄氏度 (C) 与华氏度 (F) 之间的互转
*/
public class TemperatureConverter {
/**
* 将摄氏度转换为华氏度
* @param c 摄氏温度 (double)
* @return 对应的华氏温度 (double)
*/
public static double celsiusToFahrenheit(double c) {
return c * 9.0 / 5.0 + 32.0;
}
/**
* 将华氏度转换为摄氏度
* @param f 华氏温度 (double)
* @return 对应的摄氏温度 (double)
*/
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);
// 提示用户输入
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]);
// 如果用户没输单位,默认设为 'C'
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.println("输入解析失败,请按示例输入数值与单位,例如:36.6 C");
} finally {
scanner.close();
}
}
}
Loading…
Cancel
Save