System.FormatException:'输入字符串的格式不正确。

时间:2019-05-15 19:22:44

标签: c#

我是C#的新手,我建立了一个非常基本的计算器,该计算器允许用户在两​​个文本框中输入两个数字,并通过按钮来输入一个运算符。当用户单击结果时,计算器将计算结果。

为了获得用户输入,我有:firstnumber = Double.Parse(fnum.Text);。程序编译但抛出异常System.FormatException: 'The format of the input string is incorrect.'

我也尝试过firstnumber = Convert.ToDouble(fnum.Text);,但遇到了同样的异常。

这是我的代码:

namespace Bear3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private double firstnumber;
        private double secondnumber;
        private double resultt=0;
        private bool plusch = false;
        private bool minusch = false;
        private bool multiplicationch = false;
        private bool divisionch = false;
        private void Button1_Click(object sender, EventArgs e)
        {
            Operator.Text="+";
            plusch = true;
        }

        private void Label1_Click(object sender, EventArgs e)
        {

        }

        private void Fnum_TextChanged(object sender, EventArgs e)
        {

                firstnumber = Convert.ToDouble(fnum.Text);


        }

        private void Label2_Click(object sender, EventArgs e)
        {

        }

        private void Minus_Click(object sender, EventArgs e)
        {
            Operator.Text = "-";
            minusch = true;
        }

        private void Multiplication_Click(object sender, EventArgs e)
        {
            Operator.Text = "*";
            multiplicationch = true;
        }

        private void Division_Click(object sender, EventArgs e)
        {
            Operator.Text = "/";
            divisionch = true;
        }

        private void Result_Click(object sender, EventArgs e)
        {
            if (plusch == true)
            {
                resultt = firstnumber + secondnumber;
            }
            else if (minusch == true)
            {
                resultt = firstnumber - secondnumber;
            }
            else if (multiplicationch == true)
            {
                resultt = firstnumber * secondnumber;
            }
            else if (divisionch == true)
            {
                resultt = firstnumber / secondnumber;
            }
            ResultShow.Text = Convert.ToString(resultt);

        }

        private void Snum_TextChanged(object sender, EventArgs e)
        {

                secondnumber = Double.Parse(snum.Text);


        }

        private void Clear_Click(object sender, EventArgs e)
        {
            plusch = false;
            minusch = false;
            multiplicationch = false;
            divisionch = false;
            ResultShow.Text = "";
            Operator.Text = "";
            fnum.Text = "";
            snum.Text = "";


        }
    }
}

2 个答案:

答案 0 :(得分:0)

不是在TextChanged上获取变量,而该变量可能会比您预期的触发次数更多,请在“结果”点击操作上进行操作。

将这些行移动到Result_Click的顶部,并将其从当前位置删除。

firstnumber = Convert.ToDouble(fnum.Text);   
secondnumber =Convert.ToDouble(snum.Text);

我确实建议您检查一下数据是否实际上是Double,这样就不会出错。

答案 1 :(得分:0)

更新您的Fnum_TextChangedSnum_TextChanged方法以在解析时验证文本:

private void Fnum_TextChanged(object sender, EventArgs e)
{
    if (!string.IsNullOrEmpty(fnum.Text)) 
    { 
        if (double.TryParse(fnum.Text, out firstnumber) == false){
            // notify user in message box or throw error
        }
    }
}

运行此命令后,如果没有错误,则意味着fnum.Text已解析,并且现在存储在您的firstnumber变量中。