You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
33 lines
1.0 KiB
33 lines
1.0 KiB
/**
|
|
* 温度转换器示例程序(java)
|
|
* 支持摄氏度(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) {
|
|
// 测试摄氏转华氏
|
|
double c = 36.6;
|
|
double f = celsiusToFahrenheit(c);
|
|
System.out.printf("%.2f 摄氏度 = %.2f 华氏度\n", c, f);
|
|
// 测试华氏转摄氏
|
|
f = 97.0;
|
|
c = fahrenheitToCelsius(f);
|
|
System.out.printf("%.2f 华氏度 = %.2f 摄氏度\n", f, c);
|
|
}
|
|
}
|