c#简单计算器 一个问题不懂。。

cs648413888 2012-04-03 11:27:44
问题在代码注释中 为什么 用到bool 为什么在输出小数点的时候用到bool?



public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
double a, b, result;
string action;
bool isPoint; //为什么定义 这个bool

private void button3_Click(object sender, EventArgs e)//以下是定义按钮数字
{
textBox1_ShowNum.Text += "3";
}

private void button5_Click(object sender, EventArgs e)
{
textBox1_ShowNum.Text += "5";
}

private void button7_Click(object sender, EventArgs e)
{
textBox1_ShowNum.Text += "7";
}

private void button1_Click(object sender, EventArgs e)
{
textBox1_ShowNum.Text += "1";
}

private void button2_Click(object sender, EventArgs e)
{
textBox1_ShowNum.Text += "2";
}
private void button4_Click(object sender, EventArgs e)
{
textBox1_ShowNum.Text += "4";
}
private void button6_Click(object sender, EventArgs e)
{
textBox1_ShowNum.Text += "6";
}
private void button8_Click(object sender, EventArgs e)
{
textBox1_ShowNum.Text += "8";
}

private void button9_Click(object sender, EventArgs e)
{
textBox1_ShowNum.Text += "9";
}

private void button_13_0_Click(object sender, EventArgs e)
{
textBox1_ShowNum.Text += "0";
}

private void button12_point_Click(object sender, EventArgs e)
{
if (!isPoint)//这里if 中为什么用到bool 实现的是什么功能? 为什么要 !isPoint 才 输出 "." 小数点
{
textBox1_ShowNum.Text += ".";
}
}

private void button10_加法_Click(object sender, EventArgs e)//以下是定义 加减乘除运算
{
if (textBox1_ShowNum.Text.Length != 0)
{
a = double.Parse(textBox1_ShowNum.Text);
}
else
{
a = 0;
}
action = "1";
textBox1_ShowNum.Text= " ";
}

private void button12_减法_Click(object sender, EventArgs e)
{
if (textBox1_ShowNum.Text.Length != 0)
{
a = double.Parse(textBox1_ShowNum.Text);
}
else
{
a = 0;
}
action = "2";
textBox1_ShowNum.Text= " ";
}
private void button14_乘_Click(object sender, EventArgs e)
{
if (textBox1_ShowNum.Text.Length != 0)
{
a = double.Parse(textBox1_ShowNum.Text);
}
else
{
a = 0;
}
action = "3";
textBox1_ShowNum.Text = " ";
}
private void button_除_Click(object sender, EventArgs e)
{
if (textBox1_ShowNum.Text.Length != 0)
{
a = double.Parse(textBox1_ShowNum.Text);
}
else
{
a = 0;
}
action = "4";
textBox1_ShowNum.Text = " ";
}

private void button11_等号_Click(object sender, EventArgs e)
{
if (textBox1_ShowNum.Text.Length != 0)
{
b = double.Parse(textBox1_ShowNum.Text);
}
else
{
b = 0;
}
switch (action)
{
case "1": result = a + b; break;
case "2": result = a - b; break;
case "3": result = a * b; break;
case "4":
if (b != 0)
{
result = a / b;
}
else
{
MessageBox.Show("错误");
}
break;
}
textBox1_ShowNum.Text = result.ToString();

}

private void Form1_Load(object sender, EventArgs e)
{

}



private void button17_归零_Click(object sender, EventArgs e)
{
textBox1_ShowNum.Text = " ";
}


}
...全文
126 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
liyajie865808403 2012-04-04
  • 打赏
  • 举报
回复
当第一次输入完成后要把isPoint=false;加上,不然会重复加小数点的。
liyajie865808403 2012-04-04
  • 打赏
  • 举报
回复
因为计算器只能输入一个小数点啊,判断是不是已经有小数点了
dalmeeme 2012-04-04
  • 打赏
  • 举报
回复
if (!isPoint)//这里if 中为什么用到bool 实现的是什么功能? 为什么要 !isPoint 才 输出 "." 小数点
{
textBox1_ShowNum.Text += ".";
}
仅看这段代码就明白了,第一次按“.”按钮,isPoint为false,文本框文本末尾添加个.;然后可能把isPoint设为true,这样下次再按“.”按钮,就不会添加.了。

主要是保证文本框里的数值最多只有1个小数点。
快溜 2012-04-03
  • 打赏
  • 举报
回复
应该是判断输入的数据中是否已经存在小数点,不过这个写法好像没起到什么作用。

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧