首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

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

2012-04-10 
c#简单计算器 一个问题不懂。。问题在代码注释中 为什么 用到bool 为什么在输出小数点的时候用到bool?public

c#简单计算器 一个问题不懂。。
问题在代码注释中 为什么 用到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 = " ";
  }

   
  }

[解决办法]
if (!isPoint)//这里if 中为什么用到bool 实现的是什么功能? 为什么要 !isPoint 才 输出 "." 小数点
{
textBox1_ShowNum.Text += ".";
}
仅看这段代码就明白了,第一次按“.”按钮,isPoint为false,文本框文本末尾添加个.;然后可能把isPoint设为true,这样下次再按“.”按钮,就不会添加.了。

主要是保证文本框里的数值最多只有1个小数点。

热点排行