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.

47 lines
1.5 KiB

/**
* 温度转换程序
* 支持摄氏度和华氏度的互相转换
* @author 陈全文
* @version 1.0
*/
import java.util.Scanner;
public class TemperatureConverter {
public static double celsiusToFahrenheit(double celsius) {
return celsius * 9.0 / 5.0 + 32.0;
}
public static double fahrenheitToCelsius(double fahrenheit) {
return (fahrenheit - 32.0) * 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): ");
String choice = scanner.nextLine();
if (choice.equals("1")) {
System.out.print("请输入摄氏度: ");
double celsius = scanner.nextDouble();
double fahrenheit = celsiusToFahrenheit(celsius);
System.out.println(celsius + "°C = " + fahrenheit + "°F");
} else if (choice.equals("2")) {
System.out.print("请输入华氏度: ");
double fahrenheit = scanner.nextDouble();
double celsius = fahrenheitToCelsius(fahrenheit);
System.out.println(fahrenheit + "°F = " + celsius + "°C");
} else {
System.out.println("无效选择,请输入1或2");
}
scanner.close();
}
}