用C#编一个简单计算器的代码,不知道为啥除了数字键可以使用外,清除键和操作符的都不能使用,以下是代码
privatevoidbuttonAdd_Click(objectsender,EventArgse){myOperator="add";num1=double.Pars...
private void buttonAdd_Click(object sender, EventArgs e)
{
myOperator = "add";
num1 = double.Parse(txtReadout.Text);
txtReadout.Text = "0.";
decimalFlag = false;
}
private void buttonSub_Click(object sender, EventArgs e)
{
myOperator = "Sub";
num1 = double.Parse(txtReadout.Text);
txtReadout.Text = "0.";
decimalFlag = false;
}
private void buttonMul_Click(object sender, EventArgs e)
{
myOperator = "Mul";
num1 = double.Parse(txtReadout.Text);
txtReadout.Text = "0.";
decimalFlag = false;
}
private void buttonDiv_Click(object sender, EventArgs e)
{
myOperator = "Div";
num1 = double.Parse(txtReadout.Text);
txtReadout.Text = "0.";
decimalFlag = false;
} // 【+】、【-】、【*】、【/】按钮的单击事件代码
private void buttonAmount_Click(object sender, EventArgs e)
{
num2 = double.Parse(txtReadout.Text);
switch (myOperator)
{
case "add":
result = num1 + num2;
txtReadout.Text = result.ToString();
break;
case "Sub":
result = num1 - num2;
txtReadout.Text = result.ToString();
break;
case "Mul":
result = num1 * num2;
txtReadout.Text = result.ToString();
break;
case "Div":
if (num2 == 0)
{
txtReadout.Text = "除数不能为“0”!";
}
else
{
result = num1 / num2;
txtReadout.Text = result.ToString();
}
break;
}
txtReadout.Text =result.ToString();
}
}
} 展开
{
myOperator = "add";
num1 = double.Parse(txtReadout.Text);
txtReadout.Text = "0.";
decimalFlag = false;
}
private void buttonSub_Click(object sender, EventArgs e)
{
myOperator = "Sub";
num1 = double.Parse(txtReadout.Text);
txtReadout.Text = "0.";
decimalFlag = false;
}
private void buttonMul_Click(object sender, EventArgs e)
{
myOperator = "Mul";
num1 = double.Parse(txtReadout.Text);
txtReadout.Text = "0.";
decimalFlag = false;
}
private void buttonDiv_Click(object sender, EventArgs e)
{
myOperator = "Div";
num1 = double.Parse(txtReadout.Text);
txtReadout.Text = "0.";
decimalFlag = false;
} // 【+】、【-】、【*】、【/】按钮的单击事件代码
private void buttonAmount_Click(object sender, EventArgs e)
{
num2 = double.Parse(txtReadout.Text);
switch (myOperator)
{
case "add":
result = num1 + num2;
txtReadout.Text = result.ToString();
break;
case "Sub":
result = num1 - num2;
txtReadout.Text = result.ToString();
break;
case "Mul":
result = num1 * num2;
txtReadout.Text = result.ToString();
break;
case "Div":
if (num2 == 0)
{
txtReadout.Text = "除数不能为“0”!";
}
else
{
result = num1 / num2;
txtReadout.Text = result.ToString();
}
break;
}
txtReadout.Text =result.ToString();
}
}
} 展开
3个回答
展开全部
楼主应该用窗体的key_down事件来捕捉键盘
事件里的代码应该是这样写的:
if(e.keycode==keys.espse) //捕捉到了esc键
{
//编写捕捉到esc键后的代码
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询