验证C#中的特定字符串

时间:2015-05-11 18:12:50

标签: c# validation

  public bool IsOperator(TextBox textbox, string name)
        {
            if (name != "+" || name != "-" || name != "*" || name != "/")
            {
                MessageBox.Show(name + " Must be +, -, *, or /.", "Entry Error");
                txtOperator1.Focus();
                return false;
            }
            return true;
        }

我正在制作一个基本计算器,我必须验证输入到文本框中的操作数是+, - ,*还是/。我把它编码为一个通用的验证方法已在我的程序中正确调用它。但是,当我运行程序并输入任何应该有效的操作数时,它会弹出并告诉我它们不是。我真的被困住了,非常感谢任何帮助。

编辑:这是我的代码的其他部分与此验证一起使用。其他验证器工作正常,只是IsOperator给了我悲伤。

private void btnCalculate_Click(object sender, EventArgs e)
        {
            
            try
            {
                if (IsValidData())
                {
                    decimal operand1 = Convert.ToDecimal(txtOperand1);
                    string operator1 = Convert.ToString(txtOperator1);
                    decimal operand2 = Convert.ToDecimal(txtOperand2);

                    decimal result = Calculate(operand1, operator1, operand2);

                    txtResult.Text = result.ToString("n4");
                    txtOperand1.Focus();
                }
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "\n\n" + ex.GetType().ToString() + "\n" + ex.StackTrace, "Exception");
            }
        }

public bool IsValidData()
        {
            
                IsPresent(txtOperand1, "Operand 1") &&
                IsDecimal(txtOperand1, "Operand 1") &&
                IsWithinRange(txtOperand1, "Operand 1", 1, 999999) &&

                IsPresent(txtOperator1, "Operator") &&
                IsOperator(txtOperator1, "Operator") &&

                IsPresent(txtOperand2, "Operand 2") &&
                IsDecimal(txtOperand2, "Operand 2") &&
                IsWithinRange(txtOperand2, "Operand 2", 1, 999999);
        }

private decimal Calculate(decimal operand1, string operator1, decimal operand2)
        {
            //This method performs the calculation based on what operand was entered 
            if (txtOperator1.Text == "+")
            {
                decimal calculationResult =  operand1 + operand2;
                return calculationResult;
            }

            else if (txtOperator1.Text == "-")
            {
                decimal calculationResult = operand1 - operand2;
                return calculationResult;
            }

            else if (txtOperator1.Text == "*")
            {
                decimal calculationResult = operand1 * operand2;
                return calculationResult;
            }

            else 
            {
                decimal calculationResult = operand1 / operand2;
                return calculationResult;
            }
            
        }

3 个答案:

答案 0 :(得分:2)

您需要撤消if语句逻辑

makedirs

答案 1 :(得分:0)

您正在使用||巫婆是或如果不等于+或 - 或*或/它会显示消息框。 所以,如果你做+它不等于 - min所以它仍会显示它。

答案 2 :(得分:0)

你应该使用&& (AND)而不是|| (要么)。当你使用||时(或),当其中一个条件为真时,则整个条件为真。因此,当name =“+”时,if语句的第一部分(name!=“+”)计算为false,但所有其他部分的计算结果为true,并且只有一个部分需要为整个if语句求值为true

public bool IsOperator(TextBox textbox, string name)
{
    if (name != "+" && name != "-" && name != "*" && name != "/")
    {
        MessageBox.Show(name + " Must be +, -, *, or /.", "Entry Error");
        txtOperator1.Focus();
        return false;
    }
    return true;
}

使用&& (AND)运算符,所有条件必须为真,才能使整个条件成立。

您还可以尝试以下操作,避免使用&& (AND)和|| (OR)...

private string validOperators = "+-*/";
public bool IsOperator(TextBox textbox, string name)
{
    if (!validOperator.Contains(name))
    {
        MessageBox.Show(name + " Must be +, -, *, or /.", "Entry Error");
        txtOperator1.Focus();
        return false;
    }
    return true;
}
相关问题