C语言 将华氏度转换为摄氏度的 C 程序总是打印零

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4890480/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:42:45  来源:igfitidea点击:

C program to convert Fahrenheit to Celsius always prints zero

c

提问by James

I need some help with a program for converting Fahrenheit to Celsius in C. My code looks like this

我需要在 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;
}

Every time I execute it it the result is 0.000000. I know I'm missing something but can't figure out what.

每次我执行它时,结果都是 0.000000。我知道我错过了一些东西,但无法弄清楚是什么。

回答by nybbler

5/9 will result in integer division, which will = 0

5/9 将导致整数除法,这将 = 0

Try 5.0/9.0instead.

试试吧5.0/9.0

回答by Incognito

You problem is here :

你的问题在这里:

celsius = (5/9) * (fahrenheit-32);

5/9will always give you 0. Use (5.0/9.0) instead.

5/9永远给你0。请改用 ( 5.0/9.0)。

回答by Vladimir Ivanov

try celsius = ((double)5/9) * (fahrenheit-32);Or you can use 5.0.

尝试celsius = ((double)5/9) * (fahrenheit-32);或者您可以使用 5.0。

The fact is that "/" looks at the operand type. In case of int the result is also an int, so you have 0. When 5 is treated as double, then the division will be executed correctly.

事实是“/”查看操作数类型。在 int 的情况下,结果也是一个 int,所以你有 0。当 5 被视为 double 时,那么除法将正确执行。

回答by Jonathan Wood

You need to use floating point arithmetic in order to perform these type of formulas with any accuracy. You can always convert the final result back to an integer, if needed.

您需要使用浮点运算才能以任何精度执行这些类型的公式。如果需要,您始终可以将最终结果转换回整数。

回答by Foo Bah

write 5/9.0instead of 5/9 -- this forces double division

5/9.0而不是 5/9 - 这会强制双除

回答by TheBuzzSaw

When dealing with floats, it needs to be 5.0f / 9.0f.

处理浮点数时,需要5.0f/9.0f。

When dealing with doubles, it needs to be 5.0 / 9.0.

处理双打时,需要5.0/9.0。

When dealing with integers, remainders/fractions are always truncated. 5 / 9 results between 0 and 1, so it is truncated to just 0 every time. That multiplies the other side by zero and completely nullifies your answer every time.

处理整数时,余数/分数总是被截断。5 / 9 结果介于 0 和 1 之间,因此每次都会被截断为 0。这会将另一边乘以零,并且每次都完全使您的答案无效。

回答by Pankaj Prakash

5and 9are of inttype
hence 5/9will always result 0.

5并且9int类型,
因此5/9总是会导致0

You can use 5/9.0or 5.0/9or 5.0/9.0

您可以使用5/9.05.0/95.0/9.0

You can also check C program for converting Fahrenheit into Celsius

您还可以查看C 程序将华氏温度转换为摄氏温度

回答by paul

using System;


public class Calculate
{

public static void Main(string[] args)
{
    //define variables
    int Celsius;
    int fahrenheit;
    string input;

    //prompt for input
    //read in the input and convert
    Console.WriteLine("Enter Celsius temperature");
    input = Console.ReadLine();
    Celsius = Convert.ToInt32(input);

    //calculate the result
    fahrenheit = ((Celsius * 9 )/5) + 32;

    //print to screen the result
    Console.WriteLine("32 degrees Celsius is {0}", "equivilant to 89.60 degrees fahrenheit");

    Console.ReadLine();
}