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.
 
 

28 lines
1.1 KiB

import java.util.Scanner;//使用Scanner类获取输入
public class TemperatureConverter {
public static double celsius_to_fahrenheit(double c){
return c*9.0/5.0+32.0;
}
//函数将摄氏度转换为华氏度。
public static double fahrenheit_to_celsius(double f){
return (f - 32.0) * 5.0 / 9.0;
}
//函数将华氏度转换为摄氏度。
public static void main(String[]args){
Scanner scan=new Scanner(System.in);//创建scan对象
System.out.println("请输入要转换的温度和单位:(例如 36.6 C 或 97 F)");
double temp=scan.nextDouble(); //输入双精度浮点数数值
char unit=scan.next().charAt(0); //输入浮点数后面的字符C或F(温度或华氏度)
if(unit =='C'){
double f=celsius_to_fahrenheit(temp);
System.out.printf("%.1f ℃=%.2f ℉\n",temp,f);
}
//判断温度并输出华氏度
if(unit =='F'){
double c=fahrenheit_to_celsius(temp);
System.out.printf("%.1f ℉=%.2f ℃\n",temp,c);
}
//判断华氏度并输出温度
scan.close();
}
}