import java.util.Scanner; /** * 温度转换程序 * 功能:实现摄氏温度与华氏温度之间的相互转换 */ 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; } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("===== 温度转换程序 ====="); System.out.println("1. 摄氏转华氏"); System.out.println("2. 华氏转摄氏"); System.out.print("请选择功能(1/2):"); int choice = scanner.nextInt(); double result; if (choice == 1) { System.out.print("请输入摄氏温度:"); double c = scanner.nextDouble(); result = celsiusToFahrenheit(c); System.out.printf("%.2f 摄氏 = %.2f 华氏\n", c, result); } else if (choice == 2) { System.out.print("请输入华氏温度:"); double f = scanner.nextDouble(); result = fahrenheitToCelsius(f); System.out.printf("%.2f 华氏 = %.2f 摄氏\n", f, result); } else { System.out.println("输入错误,请输入 1 或 2"); } scanner.close(); } }