Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# TryParse不工作,如果其他条件不工作_C# - Fatal编程技术网

C# TryParse不工作,如果其他条件不工作

C# TryParse不工作,如果其他条件不工作,c#,C#,我正在尝试建立一个标准形式,在这里我可以将摄氏度转换为华氏度或反之亦然 private void button1_Click(object sender, EventArgs e) { double celsius; double fahrenheit; string value; double finalValue; bool successCelsius = double.TryParse(textB

我正在尝试建立一个标准形式,在这里我可以将摄氏度转换为华氏度或反之亦然

 private void button1_Click(object sender, EventArgs e)
    {
        double celsius;
        double fahrenheit;
        string value;
        double finalValue;

        bool successCelsius = double.TryParse(textBox1.Text, out celsius);
        bool successFahrenheit = double.TryParse(textBox2.Text, out fahrenheit);
        if (successCelsius == false)
        {
            label6.Text = ("ERROR: Please enter a numeric temperature to convert.");
            return;
        }
        else
        {
            celsius = Convert.ToDouble(textBox1.Text);
            finalValue = (celsius * 9) / 5 + 32;
            label6.Text = finalValue.ToString();
            label6.Text = celsius + " " + "degrees Celsius converts to" + " " + finalValue + " " + "degrees Fahrenheit";
        }

        if (successFahrenheit == false)
        {
            label6.Text = ("ERROR: Please enter a numeric temperature to convert.");
            return;
        }
        else
        {
            fahrenheit = Convert.ToDouble(textBox2.Text);
            finalValue = (fahrenheit - 32) * 5 / 9;
            value = finalValue.ToString();
            label6.Text = fahrenheit + " " + "degrees Fahrenheit converts to" + " " + finalValue + " " + "degrees Celsius";
        }

    }
Textbox接受值并显示到label6。它检查输入的值是否为双精度值,然后工作。 idk为什么tryParse不工作


你能帮我吗?:)

此代码无法“工作”的原因是您的代码流

按照目前代码的结构,您必须同时输入摄氏度和法伦海特值,否则代码将显示“错误”。如果textbox1或textbox2为空,您的代码将从TryParse返回false。如果遵循该逻辑,将执行此代码(如果仅输入摄氏度):

更新: 你需要分别处理这两种情况……这里有一种方法很简单……检查一种,然后去做……否则,检查另一种。有很多方法可以“解决”这个问题,但这是最简单的方法之一

private void button1_Click(object sender, EventArgs e)
        {
            double celsius;
            double fahrenheit;
            string value;
            double finalValue;

            if (textBox1.Text.Length > 0)
            {
                bool successCelsius = double.TryParse(textBox1.Text, out celsius);

                if (successCelsius == false)
                {
                    label6.Text = ("ERROR: Please enter a numeric temperature to convert.");
                    return;
                }
                else
                {
                    celsius = Convert.ToDouble(textBox1.Text);
                    finalValue = (celsius * 9) / 5 + 32;
                    label6.Text = finalValue.ToString();
                    label6.Text = celsius + " " + "degrees Celsius converts to" + " " + finalValue + " " + "degrees Fahrenheit";
                }
            }
            else
            {
                bool successFahrenheit = double.TryParse(textBox2.Text, out fahrenheit);


                if (successFahrenheit == false)
                {
                    label6.Text = ("ERROR: Please enter a numeric temperature to convert.");
                    return;
                }
                else
                {
                    fahrenheit = Convert.ToDouble(textBox2.Text);
                    finalValue = (fahrenheit - 32) * 5 / 9;
                    value = finalValue.ToString();
                    label6.Text = fahrenheit + " " + "degrees Fahrenheit converts to" + " " + finalValue + " " + "degrees Celsius";
                }
            }
        }

你能扩展一下“tryParse不工作”吗?我知道代码应该做什么,但你没有说它是如何工作的。是否未输入条件的更正部分?抛出异常?您是否尝试过在调试器中单步执行它,以确认正在解析的值是您期望的值?如果是,它们是什么?您正在执行一些不必要的步骤-如果
TryParse
成功,则
out
参数(
celcius
fahrenheit
在代码中)将设置为解析值。您可以删除
celcius=Convert.ToDouble(textBox1.Text)行。您输入的值是什么?这是因为在每个
if
中都有一个
return
语句,因此如果第一个
if
条件为true,则该方法退出,而第二个
if
则不计算。如果希望方法代码继续执行,请删除
return
语句。如何修复此问题?:)如果是这样的话,你应该喜欢并接受他的回答。我试过了,上面写着‘谢谢你的反馈!声誉低于15的人所投的票将被记录,但不会改变公开显示的帖子分数
private void button1_Click(object sender, EventArgs e)
        {
            double celsius;
            double fahrenheit;
            string value;
            double finalValue;

            if (textBox1.Text.Length > 0)
            {
                bool successCelsius = double.TryParse(textBox1.Text, out celsius);

                if (successCelsius == false)
                {
                    label6.Text = ("ERROR: Please enter a numeric temperature to convert.");
                    return;
                }
                else
                {
                    celsius = Convert.ToDouble(textBox1.Text);
                    finalValue = (celsius * 9) / 5 + 32;
                    label6.Text = finalValue.ToString();
                    label6.Text = celsius + " " + "degrees Celsius converts to" + " " + finalValue + " " + "degrees Fahrenheit";
                }
            }
            else
            {
                bool successFahrenheit = double.TryParse(textBox2.Text, out fahrenheit);


                if (successFahrenheit == false)
                {
                    label6.Text = ("ERROR: Please enter a numeric temperature to convert.");
                    return;
                }
                else
                {
                    fahrenheit = Convert.ToDouble(textBox2.Text);
                    finalValue = (fahrenheit - 32) * 5 / 9;
                    value = finalValue.ToString();
                    label6.Text = fahrenheit + " " + "degrees Fahrenheit converts to" + " " + finalValue + " " + "degrees Celsius";
                }
            }
        }