我从一本 c sharp 书开始就有这个问题 这用于华氏度和摄氏度之间的转换。
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 有什么误解,但我认为这是真的? 感谢您的帮助。
最佳答案
尝试
5.0F/9.0F
否则您将使用整数运算,其中 5/9 是零。
关于c# - 华氏度和摄氏度之间的转换问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4538073/