2

私は、華氏と摂氏の間の変換のために、これを最初のcシャープブックからこの問題を抱えています。

    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を返し続けます。ここで何が問題なのですか?
問題はここにあると思うからです:

        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!?";

Order of Ops に何か問題があるのか​​もしれませんが、それは本当だと思いますか? 手伝ってくれてありがとう。

4

2 に答える 2

7

試す

5.0F/9.0F

それ以外の場合は、5/9がゼロである整数演算を使用しています。

于 2010-12-27T10:30:56.860 に答える
4

次のように、念のために別のキャストをそこに置いてみてください。

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

ほとんどの場合、5/9 は int として評価され、0 が返されます。他のオプションは次のようになります。

cel = (5f/9f)*(fahr-32);
于 2010-12-27T10:32:49.287 に答える