C# 异常循环消息框
C# Looping Messagebox for Exception
循环消息框!!
发生错误后我有一个循环消息框,我想知道如何修复它。我试过返回 Calculate() 方法,我认为这是问题所在,但我不确定。
namespace WindowsFormsApplication7
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public int division = 0;
private void Form1_Load(object sender, EventArgs e)
{
}
private decimal Calculate()
{
// This array is to hold the logical operators
string[] allowed = { "+", "-", "*", "/" };
// If the right operator is selceted then perform the action and return result
if (operate.Text == "+")
{
decimal division = Convert.ToDecimal(operand1.Text) + Convert.ToDecimal(operand2.Text);
}
else if (operate.Text == "-")
{
decimal division = Convert.ToDecimal(operand1.Text) - Convert.ToDecimal(operand2.Text);
}
else if (operate.Text == "*")
{
decimal division = Convert.ToDecimal(operand1.Text) * Convert.ToDecimal(operand2.Text);
}
else if (operate.Text == "/")
{
decimal division = (Convert.ToDecimal(operand1.Text) / Convert.ToDecimal(operand2.Text));
}
// if the operator is not something within the array then display message
else if (!allowed.Contains(operate.Text))
{
string msg = string.Format("Not a valid operater {0}Please Enter one of the Following:{0}{1}"
, Environment.NewLine, string.Join(Environment.NewLine, allowed));
MessageBox.Show(msg);
operate.Text = "";
}
return Calculate();
}
尝试
private decimal Calculate()
{
// This array is to hold the logical operators
string[] allowed = { "+", "-", "*", "/" };
decimal result = 0m;
// If the right operator is selected then perform the action and return result
if (operate.Text == "+")
{
result = Convert.ToDecimal(operand1.Text) + Convert.ToDecimal(operand2.Text);
}
else if (operate.Text == "-")
{
result = Convert.ToDecimal(operand1.Text) - Convert.ToDecimal(operand2.Text);
}
else if (operate.Text == "*")
{
result = Convert.ToDecimal(operand1.Text) * Convert.ToDecimal(operand2.Text);
}
else if (operate.Text == "/")
{
result = (Convert.ToDecimal(operand1.Text) / Convert.ToDecimal(operand2.Text));
}
// if the operator is not something within the array then display message
else if (!allowed.Contains(operate.Text))
{
string msg = string.Format("Not a valid operater {0}Please Enter one of the Following:{0}{1}"
, Environment.NewLine, string.Join(Environment.NewLine, allowed));
MessageBox.Show(msg);
operate.Text = "";
}
return result;
}
// 试试这个
private void button1_Click(object sender, EventArgs e)
{
result.Text = Calculate().ToString();
}
private decimal Calculate() {
decimal division = 0;
// This array is to hold the logical operators
string[] allowed = { "+", "-", "*", "/" };
// If the right operator is selceted then perform the action and return result
if (operate.Text == "+")
{
division = Convert.ToDecimal(operand1.Text) + Convert.ToDecimal(operand2.Text);
}
else if (operate.Text == "-")
{
division = Convert.ToDecimal(operand1.Text) - Convert.ToDecimal(operand2.Text);
}
else if (operate.Text == "*")
{
division = Convert.ToDecimal(operand1.Text) * Convert.ToDecimal(operand2.Text);
}
else if (operate.Text == "/")
{
division = (Convert.ToDecimal(operand1.Text) / Convert.ToDecimal(operand2.Text));
}
// if the operator is not something within the array then display message
else if (!allowed.Contains(operate.Text))
{
string msg = string.Format("Not a valid operater {0}Please Enter one of the Following:{0}{1}"
, Environment.NewLine, string.Join(Environment.NewLine, allowed));
MessageBox.Show(msg);
operate.Text = "";
}
return division;
}
}
循环消息框!!
发生错误后我有一个循环消息框,我想知道如何修复它。我试过返回 Calculate() 方法,我认为这是问题所在,但我不确定。
namespace WindowsFormsApplication7
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public int division = 0;
private void Form1_Load(object sender, EventArgs e)
{
}
private decimal Calculate()
{
// This array is to hold the logical operators
string[] allowed = { "+", "-", "*", "/" };
// If the right operator is selceted then perform the action and return result
if (operate.Text == "+")
{
decimal division = Convert.ToDecimal(operand1.Text) + Convert.ToDecimal(operand2.Text);
}
else if (operate.Text == "-")
{
decimal division = Convert.ToDecimal(operand1.Text) - Convert.ToDecimal(operand2.Text);
}
else if (operate.Text == "*")
{
decimal division = Convert.ToDecimal(operand1.Text) * Convert.ToDecimal(operand2.Text);
}
else if (operate.Text == "/")
{
decimal division = (Convert.ToDecimal(operand1.Text) / Convert.ToDecimal(operand2.Text));
}
// if the operator is not something within the array then display message
else if (!allowed.Contains(operate.Text))
{
string msg = string.Format("Not a valid operater {0}Please Enter one of the Following:{0}{1}"
, Environment.NewLine, string.Join(Environment.NewLine, allowed));
MessageBox.Show(msg);
operate.Text = "";
}
return Calculate();
}
尝试
private decimal Calculate()
{
// This array is to hold the logical operators
string[] allowed = { "+", "-", "*", "/" };
decimal result = 0m;
// If the right operator is selected then perform the action and return result
if (operate.Text == "+")
{
result = Convert.ToDecimal(operand1.Text) + Convert.ToDecimal(operand2.Text);
}
else if (operate.Text == "-")
{
result = Convert.ToDecimal(operand1.Text) - Convert.ToDecimal(operand2.Text);
}
else if (operate.Text == "*")
{
result = Convert.ToDecimal(operand1.Text) * Convert.ToDecimal(operand2.Text);
}
else if (operate.Text == "/")
{
result = (Convert.ToDecimal(operand1.Text) / Convert.ToDecimal(operand2.Text));
}
// if the operator is not something within the array then display message
else if (!allowed.Contains(operate.Text))
{
string msg = string.Format("Not a valid operater {0}Please Enter one of the Following:{0}{1}"
, Environment.NewLine, string.Join(Environment.NewLine, allowed));
MessageBox.Show(msg);
operate.Text = "";
}
return result;
}
// 试试这个
private void button1_Click(object sender, EventArgs e)
{
result.Text = Calculate().ToString();
}
private decimal Calculate() {
decimal division = 0;
// This array is to hold the logical operators
string[] allowed = { "+", "-", "*", "/" };
// If the right operator is selceted then perform the action and return result
if (operate.Text == "+")
{
division = Convert.ToDecimal(operand1.Text) + Convert.ToDecimal(operand2.Text);
}
else if (operate.Text == "-")
{
division = Convert.ToDecimal(operand1.Text) - Convert.ToDecimal(operand2.Text);
}
else if (operate.Text == "*")
{
division = Convert.ToDecimal(operand1.Text) * Convert.ToDecimal(operand2.Text);
}
else if (operate.Text == "/")
{
division = (Convert.ToDecimal(operand1.Text) / Convert.ToDecimal(operand2.Text));
}
// if the operator is not something within the array then display message
else if (!allowed.Contains(operate.Text))
{
string msg = string.Format("Not a valid operater {0}Please Enter one of the Following:{0}{1}"
, Environment.NewLine, string.Join(Environment.NewLine, allowed));
MessageBox.Show(msg);
operate.Text = "";
}
return division;
}
}