华氏温度和摄氏温度之间的转换 [英] Conversion Between Fahrenheit and Celsius ISSUE

查看:163
本文介绍了华氏温度和摄氏温度之间的转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从一本精锐的书开始就有这个问题 以便在华氏温度和摄氏温度之间转换.

i have this problem from a beginning c sharp book this for conversion between Fahrenheit and Celsius.

    private void button1_Click(object sender, EventArgs e)
{
    float fahr, cel;
    if (Celsius.Checked == true)
    {
        fahr = float.Parse(textBox1.Text);
        cel = (5/9)*(fahr-32);
        richTextBox1.Text = "The Degree in Celsius is:" + cel.ToString() + Environment.NewLine + "cool isn't it!?";
    }
    else if (Fahrenheit.Checked == true )
    {
        cel = float.Parse(textBox1.Text);
        fahr = ((9 * cel)/5)+ 32;
        richTextBox1.Text = "The degree in Fahrenheit is:" + fahr.ToString() + Environment.NewLine + "cool is it!?";
    }

当我想从华氏温度中获取摄氏温度时,即使公式对我来说是正确的,它也会一直给我0. 这里怎么了?
因为我认为问题出在这里:

when i want to get Celsius from a Fahrenheit it keeps giving me 0 even though the formula appears to be true to me. what's wrong here?
because i think the problem lies here:

        if (Celsius.Checked == true)
    {
        fahr = float.Parse(textBox1.Text);
        cel = (5/9)*(fahr-32);
        richTextBox1.Text = "The Degree in Celsius is:" + cel.ToString() + Environment.NewLine + "cool isn't it!?";

也许我对Ops of Ops有问题,但是我认为这是真的吗? 谢谢你的帮助.

maybe i have something wrong with Order of Ops but i think it's True? thanks for help.

推荐答案

请尝试在此处放置另一个演员,就像这样:

Try putting another cast there just to be sure, like this:

cel = ((float)5/9)*(fahr-32);

最有可能5/9计算为整数并给出0.其他选项如下:

Most probably 5/9 evaluate as ints and gives 0. Other option would be like this:

cel = (5f/9f)*(fahr-32);

这篇关于华氏温度和摄氏温度之间的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆