书中的问题:
编写一个将华氏温度转换为摄氏温度的程序,您的程序应该:
- 提示用户想要进行哪种类型的转化。
- 提示用户输入想要转换的温度。
我得到的输出不正确。我不确定哪里出了问题。我是 c 语言新手。任何帮助是极大的赞赏。 这是我的代码:
#include <stdio.h>
#include <stdlib.h>
int main()
{int f, c, f_or_c;
printf("Would you like to convert Fahrenheit (1) or Celsius (2)?\n");
scanf("%d", &f_or_c);
if(f_or_c==1)
{
printf("Enter the temperature in Fahrenheit to convert?\n");
scanf("%d", &c);
f = 1.8*c + 32.0;
printf("Celsius of %d is %d degrees.\n");
}
if(f_or_c==2)
{
printf("Enter the temperature in Celsius to convert?\n");
scanf("%d", &f);
c = (f-32)*5/9;
printf("Fahrenheit of %d is %d degrees.\n");
}
return 0;
}
最佳答案
我的猜测是您只是没有打印出值,但其他一切看起来都不错。
printf("Fahrenheit of %d is %d degrees.\n");
您没有打印任何变量。
这可能对你有用
printf("Fahrenheit of %d is %d degrees.\n", f, c);
您可以在此处查看 printf
的一般用法
http://www.cplusplus.com/reference/cstdio/printf/
关于C编程-温度转换问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24501527/