华氏度和摄氏度温度转化问题,为什么输入华氏度64,输出就成了0了,摄氏度应该是17.7777。
该回答引用GPTᴼᴾᴱᴺᴬᴵ
代码存在一些语法错误和逻辑错误,可能会导致结果不正确。下面是修正后的代码:
#include <stdio.h>
float f_to_c(float input);
float c_to_f(float input);
int main()
{
int choice;
float input, output;
printf("1 for f to c, 2 for c to f:\n");
scanf("%d", &choice);
if (choice == 1)
{
printf("please input f:\n");
scanf("%f", &input);
output = f_to_c(input);
printf("f is %.1f, c is %.1f\n", input, output);
}
else if (choice == 2)
{
printf("please input c:\n");
scanf("%f", &input);
output = c_to_f(input);
printf("c is %.1f, f is %.1f\n", input, output);
}
return 0;
}
float f_to_c(float input)
{
float output;
output = (5.0/9)*(input-32);
return output;
}
float c_to_f(float input)
{
float output;
output = (9.0/5)*input+32;
return output;
}
修正后的代码会首先要求用户输入要进行的转换类型,然后根据用户选择分别要求输入温度值,并计算输出转换后的温度值。注意在读取输入值时要使用正确的格式符,例如读取浮点数时应该使用 %f 而不是 %d。另外,输出时使用 %.1f 可以控制小数点后的位数。