使用C#的基本计算器程序
要使用C#创建计算器程序,您需要使用Web窗体。在该按钮下,创建1-9,加,减,乘等按钮。
让我们看一下用于加法,减法和乘法的代码。首先,我们声明了两个变量-
static float x, y;
现在,我们将看到如何在单个按钮单击上设置用于计算的代码:由于我们也使用Windows窗体来显示计算器,因此结果文本框为tbResult-
protected void add_Click(object sender, EventArgs e) {
x = Convert.ToInt32(tbResult.Text);
tbResult.Text = "";
y = '+';
tbResult.Text += y;
}
protected void sub_Click(object sender, EventArgs e) {
x = Convert.ToInt32(tbResult.Text);
tbResult.Text = "";
y = '-';
tbResult.Text += y;
}
protected void mul_Click(object sender, EventArgs e) {
x = Convert.ToInt32(tbResult.Text);
tbResult.Text = "";
y = '*';
tbResult.Text += y;
}以下是相等的按钮代码-
protected void eql_Click(object sender, EventArgs e) {
z = Convert.ToInt32(tbResult.Text);
tbResult.Text = "";
if (y == '/') {
p = x / z;
tbResult.Text += p;
x = d;
} else if (y == '+') {
p = x + z;
tbResult.Text += p;
a = d;
} else if (y == '-') {
p = x - z;
tbResult.Text += p;
x = p;
} else {
p = x * z;
tbResult.Text += p;
x = p;
}
}热门推荐
2 奥运会简短祝福语
10 网店宣传文案祝福语简短
11 老板提新车祝福语简短
12 简短高考状元祝福语
13 婚礼闺蜜祝福语简短
14 舞蹈培训开业祝福语简短
15 升学红包祝福语大全简短
16 理财公司新春简短祝福语
17 生日上的简短祝福语
18 猪小年经典祝福语简短