相关疑难解决方法(0)

将华氏温度转换为摄氏温度的C程序始终打印为零

我需要一些帮助,用于将华氏温度转换为C摄氏温度的程序.我的代码看起来像这样

#include <stdio.h>
int main(void)
{
    int fahrenheit;
    double celsius;

    printf("Enter the temperature in degrees fahrenheit:\n\n\n\n");
    scanf("%d", &fahrenheit);
    celsius = (5 / 9) * (fahrenheit - 32);
    printf("The converted temperature is %lf\n", celsius);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

每次执行它时,结果为0.000000.我知道我错过了什么,但无法弄清楚是什么.

c

8
推荐指数
2
解决办法
9万
查看次数

C中的华氏温度转换公式问题

在C中编写程序将摄氏温度转换为华氏温度时,以下公式给出了错误的输出:

    int fahr = 9 / 5 * celsius + 32;
Run Code Online (Sandbox Code Playgroud)

现在,我明白这可能是一个问题,9/5被解释为一个整数,但我不明白的是使用doublefloat它仍然给出相同的错误输出.

奇怪的是,尽管将类型设置为以下公式,但以下公式给出了正确的输出int:

int fahr = celsius / 5 * 9 + 32;
Run Code Online (Sandbox Code Playgroud)

此外,我注意到甚至像下面这样简单的东西,当类型设置为时double,仍然输出为1.0而不是1.8:

 double x = 9 / 5;
 printf("%lf\n", x);
Run Code Online (Sandbox Code Playgroud)

我读过这个帖子:

C程序将华氏温度转换为摄氏温度

但我仍然不明白为什么int fahr = celsius / 5 * 9 + 32;有效而不是int fahr = 9/5 * celsius+32;

c

3
推荐指数
1
解决办法
397
查看次数

标签 统计

c ×2