问题描述
int main (void)
{
int fahrenheit; // fahrenheit stands for fahrenheit
double c; // c stands for celsius
printf("Enter your fahrenheit, we'll covnvert it into celsius! ");
scanf("%f", &fahrenheit);
c = 5/9 * (fahrenheit - 32);
printf("Here is your %f in celsius!.\n");
return (0);
}
我跟着突破点code,当它发生在我的输入计算关闭,但公式是正确的。某种逻辑错误,我不能把我的手指上。请帮助!
I've followed the code through break points and when it takes in my input the calculations are off, but the formula is correct. Some sort of logic error I can't put my finger on. Please help!
推荐答案
的 scanf函数
呼叫使用了错误的格式字符串。您正在阅读的 INT
,所以你需要它是:
The scanf
call uses the wrong format string. You are reading an int
so you need it to be:
scanf("%d", &fahrenheit);
这位前pression 5/9
用整数除法计算。事实上,编译器在编译的时候做出来。这当然pression计算结果为 0
。
The expression 5/9
is evaluated using integer division. In fact the compiler can work it out at compile time. That expression evaluates to 0
.
您需要执行浮点除法。例如:
You need to perform floating point division. For instance:
5.0/9
或者
5/9.0
或者
5.0/9.0
您只需要至少一个操作数是浮点值。
You just need at least one operand to be a floating point value.
把这个放入你的前pression,你可以写:
Putting this into your expression, you can write:
c = 5.0/9.0 * (fahrenheit - 32);
和得到您所期望的答案。
and obtain the answer that you expect.
您的printf
说法是错误的了。您应该启用警告,并让编译器告诉你。你的意思写的:
Your printf
statement is wrong too. You should enable warnings and let the compiler tell you that. You meant to write:
printf("Here is your %f in celsius!.\n", c);
这篇关于C:转换到华氏摄氏度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!