{“输入字符串的格式不正确。”}

时间:2015-11-24 11:07:23

标签: c# split switch-statement case calculator

我无法弄清楚我得到的错误

“{”输入字符串的格式不正确。“}”

当我输入例如5 + 5进入我的Sum文本框并点击等于然后应该在我的答案文本框中显示我的答案时,然而当我输入5 + 5时我得到上面的错误显示我似乎无法解决它。

这是我的添加按钮代码

    private void Addition_Click(object sender, EventArgs e)
    {
        dbl_FirstNumber = Convert.ToDouble(Sum.Text); //Operation Works, need to get rid of the 0
        Sum.AppendText(" + ");

这是我的等号按钮代码

    private void equals_Click(object sender, EventArgs e)
    {
        string[] hold = Sum.Text.Split(' ');//
        switch (hold[1])//holds the first number

        {
            case "+":
                Result.Text = Convert.ToString(Convert.ToDouble(Sum.Text) + hold[0].ToString() + hold[2].ToString());   
                break;   

我的第二个问题

好的,所以当我尝试输入3 + 3 + 3时,我得到了错误 “{”输入字符串的格式不正确。“}”

这是我的附加代码

     private void Addition_Click(object sender, EventArgs e)
    {
        dbl_FirstNumber = Convert.ToDouble(Sum.Text); 
        Sum.AppendText(" + ");

    }

错误的屏幕截图 - https://gyazo.com/9689d4f5055d7cc9211e1c2822b1d6f3

1 个答案:

答案 0 :(得分:0)

有错误......

private void equals_Click(object sender, EventArgs e)
{
    string[] hold = Sum.Text.Split(' ');
    switch (hold[1]) // holds the DELIMTER
    {
        case "+":
            Result.Text = Convert.ToString(Convert.ToDouble(Sum.Text) + hold[0].ToString() + hold[2].ToString());   
            // THATS WRONG!                                 ^ this would take "5 + 5" and you CANNOT convert this.
            // here you would Need: 
            Result.Text = (Convert.ToDouble(hold[0]) + Convert.ToDouble(hold[2])).ToString();

            break;