gpt4 book ai didi

c# - 名称 'result'在当前上下文中不存在

转载 作者:行者123 更新时间:2023-12-02 10:50:31 26 4
gpt4 key购买 nike

现在,当我运行该程序时,即使我输入了正确的operator,它也会给我operator错误消息。有任何想法吗?
也感谢你们的结果修复。非常感谢:)我在想我以某种方式滥用了IsValidData()方法定义的代码,并在IsOperator(txtOperator, "+,-,*,/")区域中将其称为错误。

   ` private void textBox1_TextChanged(object sender, EventArgs e)
{

}

private void btnCalculate_Click(object sender, EventArgs e)
{

try
{
//Set Validation
if(IsValidData())
{

decimal Operand1 = Convert.ToDecimal (txtOperand1.Text);
string Operator = Convert.ToString (txtOperator.Text);
decimal Operand2 = Convert.ToDecimal(txtOperand2.Text);
decimal result = Calculate(Operand1, Operator, Operand2);

txtResult.Text = result.ToString("f4");
txtOperand1.Focus();
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message + "\n\n" + ex.GetType().ToString() + "\n" + ex.StackTrace, "Exception");
}
}
//Set IsValidData()
public bool IsValidData()
{
return
IsPresent(txtOperand1, "Operand 1") &&
IsDecimal(txtOperand1, "Operand 1") &&
IsWithinRange(txtOperand1, "Operand 1", 0, 1000000) &&

IsPresent(txtOperator, "Operator") &&
IsOperator(txtOperator, "+,-,*,/") &&

IsPresent(txtOperand2, "Operand 2") &&
IsDecimal(txtOperand2, "Operand 2") &&
IsWithinRange(txtOperand2, "Operand 2", 0, 1000000);
}
//Setup IsPresent
public bool IsPresent(TextBox textBox, string name)
{
if (textBox.Text == "")
{
MessageBox.Show(name + " is required to continue.", "Entry Error");
textBox.Focus();
return false;
}
return true;

}
//Setup IsDecimal
public bool IsDecimal(TextBox textBox, string name)
{
try
{
Convert.ToDecimal(textBox.Text);
return true;
}
catch (FormatException)
{
MessageBox.Show(name + " must be a decimal value.", "Entry Error");
textBox.Focus();
return false;
}
}
//Setup IsOperator
public bool IsOperator(TextBox textBox, string operators)
{
try
{
foreach (string s in operators.Split(new char[] { ',' }))
{
if (textBox.Text.Trim() == s)
return true;
else
throw new ArgumentException("The operator must be a valid operator: +,-, *, /", "name");
}
return true;

}
catch (ArgumentException ex)
{
MessageBox.Show(ex.Message);
txtOperator.Focus();
return false;
}
}
//Setup IsWithinRange.
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 + " and " + max + ".", "Entry Error");
textBox.Focus();
return false;
}
return true;
}
//Setup Calculate Method.
private decimal Calculate(decimal Operand1, string Operator, decimal Operand2)
{
if (Operator == "+")
{
decimal result = Operand1 + Operand2;
}
else if (Operator == "-")
{
decimal result = Operand1 - Operand2;
}
else if (Operator == "*")
{
decimal result = Operand1 * Operand2;
}
else
{
decimal result = Operand1 / Operand2;
}
return result;
}

private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
}`

最佳答案

您需要在if/else语句范围之外声明变量。在If/Else语句内定义变量时,一旦函数退出该if/else语句,该变量将失去作用域。

decimal result;
if (Operator == "+")
{
result = Operand1 + Operand2;
}
else if (Operator == "-")
{
result = Operand1 - Operand2;
}
else if (Operator == "*")
{
result = Operand1 * Operand2;
}
else
{
result = Operand1 / Operand2;
}
return result;

关于c# - 名称 'result'在当前上下文中不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22367960/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com