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.

39 lines
1.2 KiB

/**
* 温度转换程序:实现摄氏温度与华氏温度的双向转换
*/
public class TemperatureConverter {
/**
* 摄氏温度转华氏温度
* @param celsius 待转换的摄氏温度值
* @return 转换后的华氏温度值
*/
public static double celsiusToFahrenheit(double celsius) {
return celsius * 9.0 / 5.0 + 32;
}
/**
* 华氏温度转摄氏温度
* @param fahrenheit 待转换的华氏温度值
* @return 转换后的摄氏温度值
*/
public static double fahrenheitToCelsius(double fahrenheit) {
return (fahrenheit - 32) * 5.0 / 9.0;
}
/**
* 主方法:程序入口,演示转换功能
* @param args 命令行参数
*/
public static void main(String[] args) {
// 示例1:摄氏转华氏
double c = 36.6;
double f = celsiusToFahrenheit(c);
System.out.printf("%.2f 摄氏度 = %.2f 华氏度%n", c, f);
// 示例2:华氏转摄氏
double f2 = 98.6;
double c2 = fahrenheitToCelsius(f2);
System.out.printf("%.2f 华氏度 = %.2f 摄氏度%n", f2, c2);
}
}