我正在尝试使用类似于计算器的C#制作表单。
我必须编写一个名为IsOperator的方法,该方法检查传递给它的文本框是否包含+,-,*或/值。
由于某种原因,它无法正确验证。
我尝试更改||到&&并将返回的结果反转为false和true,但没有任何效果。
当我尝试在运算符文本框中放入不是+ /-*的其他内容时,结果将变为0。没有任何结果可以验证。
代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SimpleCalculator
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnCalc_Click(object sender, EventArgs e)
{
try
{
if (IsValidData())
{
MessageBox.Show("Error");
}
else
{
decimal operand1 = Convert.ToDecimal(txtOperand1.Text);
string operator1 = txtOperator.Text;
decimal operand2 = Convert.ToDecimal(txtOperand2.Text);
decimal result = 0;
if (operator1 == "+")
result = operand1 + operand2;
else if (operator1 == "-")
result = operand1 - operand2;
else if (operator1 == "*")
result = operand1 * operand2;
else if (operator1 == "/")
result = operand1 / operand2;
result = Math.Round(result, 4);
txtResult.Text = result.ToString();
txtOperand1.Focus();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + "\n\n" +
ex.GetType().ToString() + "\n" +
ex.StackTrace, "Exception");
}
}
public bool IsValidData()
{
return
//validate the operand1 text box
IsPresent(txtOperand1, "Operand 1") &&
IsDecimal(txtOperand1, "Operand 1") &&
IsWithinRange(txtOperand1, "Operand 1", 0, 1000000) &&
//validates the operator text box
IsPresent(txtOperator, "Operator") &&
IsOperator(txtOperator, "Operator") &&
//validates the operand 2 text box
IsPresent(txtOperand2, "Operand 2") &&
IsDecimal(txtOperand2, "Operand 2") &&
IsWithinRange(txtOperand2, "Operand 2", 0, 1000000);
}
private void btnExit_Click(object sender, System.EventArgs e)
{
this.Close();
}
//is present
public bool IsPresent(TextBox textBox, string name)
{
if (textBox.Text == "")
{
MessageBox.Show(name + " is a required field.", "Entry Error");
textBox.Focus();
}
return false;
}
//is decimal
public bool IsDecimal(TextBox textBox, string name)
{
decimal number = 0m;
if (Decimal.TryParse(textBox.Text, out number))
{
return true;
}
else
{
MessageBox.Show(name + " must be a decimal value.", "Entry Error");
textBox.Focus();
return false;
}
}
//is within range
public bool IsWithinRange(TextBox textBox, string name, decimal min, decimal max)
{
decimal number = Convert.ToDecimal(textBox.Text);
if (number < min || number > max)
{
MessageBox.Show(name + "must be between" + min.ToString()
+ "and" + max.ToString() + ".", "Entry Error");
textBox.Focus();
return false;
}
return true;
}
//is a valid operator
public bool IsOperator(TextBox textBox, string name)
{
string operator1 = "";
operator1 = Convert.ToString(textBox.Text);
if (operator1 == "+" && operator1 == "-" && operator1 == "/" && operator1 == "*")
{
MessageBox.Show("Please enter a valid operator in the operator text box.", "Entry Error");
return false;
}
return true;
}
private void txtOperand1_TextChanged(object sender, EventArgs e)
{
this.txtResult.Text = "";
}
private void txtOperator_TextChanged(object sender, EventArgs e)
{
this.txtResult.Text = "";
}
private void txtOperand2_TextChanged(object sender, EventArgs e)
{
this.txtResult.Text = "";
}
}
}
答案 0 :(得分:0)
所有拳头都不应该使用someString == ""
或string.IsNullOrEmpty(someString)
或string.IsEmptyOrWhitespace(someString)
。 1
然后IsPresent
总是返回false
。您可以将该方法更改为
public bool IsPresent(TextBox textBox, string name)
{
if (!string.IsNullOrEmpty(textBox.Text))
{
return true;
}
MessageBox.Show(name + " is a required field.", "Entry Error");
textBox.Focus();
return false;
}
在EventHandler中,您在!
之前忘记了IsValidData()
。当数据有效时,您将显示错误;当数据有故障时,您将尝试执行计算。
您的IsOperator
方法实际上包含一个逻辑问题。如果运算符是以下任何字符+, -, *, \
,则要返回true。因此,使用LINQ将if
逻辑翻转为类似的东西比较容易
//is a valid operator
public bool IsOperator(TextBox textBox, string name)
{
string operator = textBox.Text;
if (new[] {"+", "-", "*", "/"}.Contains(operator))
{
return true;
}
MessageBox.Show("Please enter a valid operator in the operator text box.", "Entry Error");
return false;
}
我认为您的代码也可以从使用异常中受益,而不是在发生错误时立即显示MessageBoxes。