使用小数时获取 System.FormatException,似乎我不应该



我需要制作一个"银行"程序,除了一个问题之外,我还设置了它并正常工作。当试图提取比帐户中更多的钱时,我不断收到一个System.FormatException告诉我"输入字符串格式不正确"。我正在使用十进制变量,例如当我在文本框中输入 101 以从包含 100 美元的帐户中提取时,我得到异常。

我想向用户发出警告,说明他们没有足够的资金,然后将帐户金额恢复到他们尝试提款之前的状态。我该怎么做呢?我在网上看了很多地方,但没有找到太多关于这个问题的帮助。也请放轻松,我对编程还很陌生。

ps. 当存款或取款而不变为负数时,这不是问题,只有当账户变为负数时才会发生异常。

if (amount < 0m)
        {
            amount += Decimal.Parse(textBox1.Text); // this is where the error occurs 
            MessageBox.Show("invalid funds");
        }

完整代码在这里

 Decimal amount = 0.00m;
    public Form1()
    {
        InitializeComponent();
    }
    private void Deposit_Click(object sender, EventArgs e)
    {
            try
            {
                //checks for a user input below or equal to 1000 and greater than 0
                if (Decimal.Parse(textBox1.Text) <= 1000 && Decimal.Parse(textBox1.Text) > 0)
                {
                    amount += Decimal.Parse(textBox1.Text);
                    textBox1.Text = "";
                }
            else
            {
                {
                    // shows when ever a user trys to deposit anything other than a valid number
                    MessageBox.Show("Please enter an amount between $0 - $1,000.");
                    textBox1.Text = "";
                }
            }
            }
            // catches exceptions when the user trys to deposit anything other than a valid number
            catch
            {
                MessageBox.Show("Please enter an amount between $0 - $1,000.");
                textBox1.Text = "";
            }
    }
    private void Account_Click(object sender, EventArgs e)
    {
        textBox1.Text = string.Format("$" + amount.ToString());
    }
    private void Withdrawal_Click(object sender, EventArgs e)
    {
        try
        {
            //checks for a user input below or equal to 1000
            if (Decimal.Parse(textBox1.Text) <= 1000 && Decimal.Parse(textBox1.Text) > 0)
            {
                amount -= Decimal.Parse(textBox1.Text);
                textBox1.Text = "";
            }
            else
            {
                {
                    // shows when ever a user trys to withdrawal anything other than a valid number
                    MessageBox.Show("Please enter an amount between $0 - $1,000.");
                    textBox1.Text = "";
                }
            }
        }
        // catches exceptions when the user trys to withdrawal anything other than a valid number
        catch
        {
            MessageBox.Show("Please enter an amount between $0 - $1,000.");
            textBox1.Text = "";
        }
        //checks to see if the user has withdrawn the account below 0
        if (amount < 0m)
        {
            amount += Decimal.Parse(textBox1.Text);
            MessageBox.Show("invalid funds");
        }
    }

    //clears the text box for a new entry when the user click on it
    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        textBox1.Text = "";
    }

有时(Account_Click(输出 textBox1 可能没有帮助自己,但在其他时候它不应该有美元符号(只是 Withdrawal_Click 中的数字(。

这种事情会搅浑水,不仅使应用程序的使用变得混乱,而且还会在调试时使您感到困惑 - 也许不是在这种情况下,而是在您编写更复杂的程序时。

发生异常是因为文本框中有一个字符对小数无效(我猜是 $(。

你要做的是,当Visual Studio因异常而停止时,将鼠标悬停在"textBox1.Text"上,看看它的值是什么,它是否有"$"或任何空格?

相关内容

最新更新