1

如何将这些错误检查放入方法中?用户不能使程序崩溃,数值必须在 0 到 1,000,000 之间。这就是我到目前为止所拥有的。

如果有一个空字段,则必须有一条错误消息说:请输入缺少的字段

    private void btnCalculate_Click(object sender, EventArgs e)
    {
        try
        {
            // changes value that is in text box and switches it into a decimal
            decimal firstNumber = Convert.ToDecimal(txtOperand1.Text);
            decimal secondNumber = Convert.ToDecimal(txtOperand2.Text);
            string mathSymbol = txtOperator.Text;   // already a string variable
            decimal result = 0;

            //calls the method
            result = calculate(firstNumber, secondNumber, mathSymbol, result);



            if (mathSymbol != "/" && mathSymbol != "*" && mathSymbol != "+" && mathSymbol != "-")
            {
                txtOperator.Text = ""; 
                MessageBox.Show("ERROR invalid operator");

            }



            if (firstNumber <= 0 || firstNumber >= 100000 || secondNumber <= 0 || secondNumber >= 1000000)
            {
                txtResult.Text = ""; 
                MessageBox.Show("Numbers must be greater than 0 and less than 1,000,000");
                txtOperand1.Text = "";
                txtOperand2.Text = "";
                txtOperator.Text = "";                
            }

        }
        catch (FormatException)
        {
            MessageBox.Show("Please enter values.", "ERROR");
        }
        catch (Exception op)
        {
            MessageBox.Show(op.Message, "ERROR");
        }
    }

    // method calculate - amount = to calculate then calculate = to result
    private decimal calculate(decimal num1, decimal num2, string symbol, decimal amount)
       {    
            if (symbol == "+")      // checks if user enters a + then adds numbers
                amount = num1 + num2;

            else if (symbol == "-") // checks if user enters a - then minus numbers
                amount = num1 - num2;

            else if (symbol == "*") // checks if user enters a * then multiplies numbers
                amount = num1 * num2;

           else if (symbol == "/")  // checks if user enters a / then divides numbers
                amount = num1 / num2;

            txtResult.Text = amount.ToString("f4");

           return amount;
       }






    private void btnExit_Click(object sender, EventArgs e)
    {
        this.Close();   // closes the form if exit button is pressed
    }

    private void clearResult(object sender, EventArgs e)
    {
        txtResult.Text = " "; // clears results if txtOperand1 is changed
    }

    private void clear_result(object sender, EventArgs e)
    {
        txtResult.Text = " "; // clears results if txtOperator is changed
     }

    private void ClearResults3(object sender, EventArgs e)
    {
        txtResult.Text = " "; // clears results if txtOperand2 is changed
    }
}
4

2 回答 2

2

通常,如果数字无效,您将使用Decimal.TryParse转换并输出消息。例如:

decimal firstNumber;
decimal secondNumber;

if (!decimal.TryParse(txtOperand1.Text, out firstNumber))
{
    MessageBox.Show("Number is not valid.");
    return;
}
if (firstNumber <= 0 || firstNumber >= 1000000)
{
    MessageBox.Show("Number must be > 0 and < 1000000");
    return;
}

您可以对第二个数字执行类似的操作。

或者,只需将整个事情包装在一个方法中并调用它,让它返回 abool以指示数字是否有效。

于 2013-09-02T20:49:25.070 回答
1

这是我的方法

private int ValidInput(string operand1, string operand2, string mathSymbol, out decimal firstNumber, out decimal secondNumber out string errorMsg)
{
    int errorCode  ;

    errorMsg = string.Emtpy ;
    firstNumber = 0 ;
    secondNumber = 0 ;

    try 
    {
        if ((mathSymbol != "/") && (mathSymbol != "*") && (mathSymbol != "+") && (mathSymbol != "-"))
        {
            errorCode = 1 ;
            errorMsg = "ERROR invalid operator";
        }   
        else
        {
            errorCode = 2 ;
            firstNumber = Convert.ToDecimal(operand1);

            if (firstNumber <= 0 || firstNumber >= 1000000)
            {
                errorCode = 3 ;
                errorMsg = "first number must be greater than 0 and less than 1,000,000" ;
            }
            else
            {
                errorCode = 4 ;
                secondNumber = Convert.ToDecimal(operand2);

                if (secondNumber <= 0 || secondNumber >= 1000000)
                {   
                    errorCode = 5 ;
                    errorMsg = "second number must be greater than 0 and less than 1,000,000" ;
                }
                else
                {
                    errorCode = 0;
                }
            }

        }
    }
    catch (FormatException  fe)
    {
        errorMsg = fe.Message ;
    }
    catch (OverflowException oe)
    {
        errorMsg = oe.Message ;
    }
    return errorCode
}
private void btnCalculate_Click(object sender, EventArgs e) 
{
    decimal firstNumber ;
    decimal secondNumber ;
    string mathSymbol = txtOperator.Text;   // already a string variable
    decimal result = 0;
    int errorCode ;
            string errorMsg ;

    errorCode = ValidInput(txtOperand1.Text.Trim(), txtOperand2.Text.Trim(), mathSymbol, out firstNumber, out secondNumber out errorMsg) ;

    //if there was no error
    if(errorCode == 0)
    {
        //calls the calculate method
        result = calculate(firstNumber, secondNumber, mathSymbol, result);

        //you can use the errorCode number to decide which fields to clear or provide more usefull message
    }
    else
    {
        MessageBox.Show(errorMsg, "ERROR");
    }
}

顺便说一句,在您的错误消息中,您说的是“1,000.000”,但在您的代码中,您有“100000”

于 2013-09-02T20:59:17.953 回答