简体   繁体   English

验证C#中的特定字符串

[英]Validating a specific string in C#

  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;
        }

I am making a basic calculator and I have to validate that the operand entered into the text box is either a +, -, *, or /. 我正在制作一个基本的计算器,我必须验证输入到文本框中的操作数是+,-,*或/。 I coded this as a generic validating method have correctly called it in my program. 我将此编码为通用验证方法,已在程序中正确调用它。 However, when I run the program and enter any of the operands that should be valid it pops up and tells me they're not. 但是,当我运行程序并输入应该有效的任何操作数时,它会弹出并告诉我它们不是。 I'm really stuck and would greatly appreciate any help. 我真的很困,不胜感激。

Edit: Here is the other parts of my code that goes with this validation. 编辑:这是我的代码的其他部分与此验证一起。 The other validators are working fine it's just the IsOperator that is giving me grief. 其他验证器运行良好,只是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; } } 

You need to reverse your if statement logic 您需要反转if语句逻辑

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

You are using || 您正在使用|| witch is Or so if it is not equal to + or - or * or / it will show you the message box. 巫婆是或因此,如果它不等于+或-或*或/,它将显示消息框。 so if you do + it is not equal to - min so it will still show it. 因此,如果您执行+,则它不等于-分钟,因此仍会显示它。

You should use && (AND) instead of || 您应使用&&(AND)代替|| (OR). (要么)。 When you're using || 当您使用||时 (OR), when one of those conditions is true, then the whole condition is true. (OR),当其中一个条件为真时,则整个条件为真。 So when name = "+", the first part of your if statement (name != "+") evaluates false, but all of the other parts evaluate true, and only one needs to evaluate true for the entire if statement to be true. 因此,当name =“ +”时,if语句的第一部分(name!=“ +”)的计算结果为false,但其他所有部分的计算结果为true,对于整个if语句为true的情况,只有一个评估结果为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;
}

Using the && (AND) operator, all conditions must be true for the whole condition to be true. 使用&&(AND)运算符,所有条件都必须为真,整个条件才能为真。

You could also try the following, which avoids using && (AND) 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM