1 changed files with 44 additions and 0 deletions
@ -0,0 +1,44 @@ |
|||
/** |
|||
* 温度转换工具类 |
|||
* 功能:实现摄氏温度(Celsius)与华氏温度(Fahrenheit)的双向转换 |
|||
*/ |
|||
public class TemperatureConverter { |
|||
|
|||
/** |
|||
* 摄氏温度 转换为 华氏温度 |
|||
* 转换公式:F = C * 9 / 5 + 32 |
|||
* |
|||
* @param celsius 输入的摄氏温度值 |
|||
* @return 转换后的华氏温度 |
|||
*/ |
|||
public static double convertCelsiusToFahrenheit(double celsius) { |
|||
return celsius * 9 / 5 + 32; |
|||
} |
|||
|
|||
/** |
|||
* 华氏温度 转换为 摄氏温度 |
|||
* 转换公式:C = (F - 32) * 5 / 9 |
|||
* |
|||
* @param fahrenheit 输入的华氏温度值 |
|||
* @return 转换后的摄氏温度 |
|||
*/ |
|||
public static double convertFahrenheitToCelsius(double fahrenheit) { |
|||
return (fahrenheit - 32) * 5 / 9; |
|||
} |
|||
|
|||
/** |
|||
* 程序入口 main 方法 |
|||
* 演示基础的温度转换功能 |
|||
*/ |
|||
public static void main(String[] args) { |
|||
// 示例:36.6摄氏度 转 华氏度
|
|||
double cTemp = 36.6; |
|||
double fResult = convertCelsiusToFahrenheit(cTemp); |
|||
System.out.printf("%.1f 摄氏度 = %.1f 华氏度%n", cTemp, fResult); |
|||
|
|||
// 示例:98.6华氏度 转 摄氏度
|
|||
double fTemp = 98.6; |
|||
double cResult = convertFahrenheitToCelsius(fTemp); |
|||
System.out.printf("%.1f 华氏度 = %.1f 摄氏度%n", fTemp, cResult); |
|||
} |
|||
} |
|||
Loading…
Reference in new issue