分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.youkuaiyun.com/jiangjunshow
也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!
C#之WinForm程序设计-简单计算器
在这个小程序中我们需要用到的组件有:
- Button:点击计算
- TextBox:输出要运算的数
- RadioButton:选择运算类型
- GroupBox:绑定RadioButton
首先我们在界面上拖以上的控件,得到如下界面:
这时候监听计算按钮的点击事件:
private void button1_Click(object sender, EventArgs e) { double op1, op2, result; if (textBox1.Text == ""||textBox2.Text=="" ) {//判断是否两个框框都输入了数据 MessageBox.Show(this,"输入错误","msg",MessageBoxButtons.OK, MessageBoxIcon.Information);//有空余项没输入数据弹出提示框 return; } op1 = double.Parse(textBox1.Text);//得到两个框框的值并转化为long类型 op2 = double.Parse(textBox2.Text); if (radioButton1.Checked) {//加法 result = op1 + op2; } else if (radioButton2.Checked){//减法 result = op1 - op2; } else if (radioButton3.Checked){//乘法 result = op1 * op2; } else {//除法 result = op1 / op2; } textBox3.Text = result.ToString();//设置textBox3的值 }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
我们看一下测试的结果:
加法:
乘法:
好了,上面基本就是一个简单的计算器的例子了!
给我老师的人工智能教程打call!http://blog.youkuaiyun.com/jiangjunshow
