首页 新闻 会员 周边 捐助

为什么第一次点减运算能运算第二次之后点其他运算符后一直加怎么修改,新手帮下哈

0
[已关闭问题] 关闭于 2013-04-17 22:37

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SimpleCalculator
{
public partial class frmCalculator : Form
{
private int left = 0;
private int right =0;
private string oper = "+";
private bool isOperator = false;

public frmCalculator()
{
InitializeComponent();
}

private void btnEqual_Click(object sender, EventArgs e)
{
try
{
switch (oper)
{
case "+":
left = left + right;
break;
case "-":
left = left - right;
break;
case "*":
left = left * right;
break;
case "/":
left = left / right;
break;
default:
break;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
tbShow.Text = Convert.ToString(left);
right = left;
isOperator = false;
oper = "";
}

private void btnNum_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
if (isOperator)
{
right +=Convert.ToInt32( btn.Text);
tbShow.Text = right.ToString();
}
else
{
left +=Convert.ToInt32( btn.Text);
tbShow.Text = left.ToString();
}

}

private void btnOperator_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
oper = btn.Text;
isOperator = true;
}
}

Aiolos丶M的主页 Aiolos丶M | 菜鸟二级 | 园豆:353
提问于:2013-04-16 22:01
< >
分享
所有回答(1)
0

private void btnEqual_Click(object sender, EventArgs e)
{
try
{
switch (oper)
{
case "+":
left = left + right;
break;
case "-":
left = left - right;
break;
case "*":
left = left * right;
break;
case "/":
left = left / right;
break;
default:
break;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
tbShow.Text = Convert.ToString(left);
right = left;
isOperator = false;
oper = "";
}中最后加一条right=0;即可

Aiolos丶M | 园豆:353 (菜鸟二级) | 2013-04-17 22:36
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册