C#WPF计算器!一个刚刚入门一个星期Csharp的应届生!
栏目:应届生落户 人气:0 发布日期:2023-02-02 来源:网络
(C#WPF计算器!一个刚刚入门一个星期Csharp的应届生!)
先入正题吧!
1:创建wpf应用。我用的VS是2013!
2:创建wpf文件并且起名字!
3:起一条虚线
3:打开我们电脑基本的计算器,然后我们模仿他来做计算器吧!
4:在工具箱找到lable,把他拖进来。并且起一个名字!设置他的属性。
4:再然后在工具箱找到button按钮把他拖进来。并且帮他起名字,还有设置他的属性!
5:写完整的界面,按钮直接复制粘贴就可以的!下一步进行后台代码!
6:定义三个变量存放数据!
7:设置按钮0的点击事件!然后数字键复制粘贴就可以了!
8:设置符号键,给他赋值!
9:设置C按键清除所有!这个问了很多人才找到的C按键清除lable!
10:设置等于号按键促发加减乘除!
11:然后一个计算器完成了!下面是全部代码!
namespaceLVcalculation
{
??///
??///MainWindow.xaml的交互逻辑
??///
??publicpartialclassMainWindow:Window
??{
????stringnumber1=null,number2=null,flag=null;//第一个变量存放按下的第一个字符,第二个变量存放按下符号键的字符。第三个存放符号的字符!
????publicMainWindow左边()右边
????{
??????InitializeComponent左边()右边;
????}
????privatevoidbutton0_Click(objectsender,RoutedEventArgse)
????{
??????if(flag==null)
??????{
????????number1=number1+"0";
????????lable1.Content=number1;
??????}
??????else{
????????number2=number2+"0";
????????lable1.Content=number2;
???????
??????}
????}
????privatevoidbutton1_Click(objectsender,RoutedEventArgse)
????{
??????if(flag==null)
??????{
????????number1=number1+"1";
????????lable1.Content=number1;
??????}
??????else
??????{
????????number2=number2+"1";
????????lable1.Content=number2;
??????}
????}
????privatevoidbutton2_Click(objectsender,RoutedEventArgse)
????{
??????if(flag==null)
??????{
????????number1=number1+"2";
????????lable1.Content=number1;
??????}
??????else
??????{
????????number2=number2+"2";
????????lable1.Content=number2;
??????}
????}
????privatevoidbutton3_Click(objectsender,RoutedEventArgse)
????{
??????if(flag==null)
??????{
????????number1=number1+"3";
????????lable1.Content=number1;
??????}
??????else
??????{
????????number2=number2+"3";
????????lable1.Content=number2;
??????}
????}
????privatevoidbutton4_Click(objectsender,RoutedEventArgse)
????{
??????if(flag==null)
??????{
????????number1=number1+"4";
????????lable1.Content=number1;
??????}
??????else
??????{
????????number2=number2+"4";
????????lable1.Content=number2;
??????}
????}
????privatevoidbutton5_Click(objectsender,RoutedEventArgse)
????{
??????if(flag==null)
??????{
????????number1=number1+"5";
????????lable1.Content=number1;
??????}
??????else
??????{
????????number2=number2+"5";
????????lable1.Content=number2;
??????}
????}
????privatevoidbutton6_Click(objectsender,RoutedEventArgse)
????{
??????if(flag==null)
??????{
????????number1=number1+"6";
????????lable1.Content=number1;
??????}
??????else
??????{
????????number2=number2+"6";
????????lable1.Content=number2;
??????}
????}
????privatevoidbutton7_Click(objectsender,RoutedEventArgse)
????{
??????if(flag==null)
??????{
????????number1=number1+"7";
????????lable1.Content=number1;
??????}
??????else
??????{
????????number2=number2+"7";
????????lable1.Content=number2;
??????}
????}
????privatevoidbutton8_Click(objectsender,RoutedEventArgse)
????{
??????if(flag==null)
??????{
????????number1=number1+"8";
????????lable1.Content=number1;
??????}
??????else
??????{
????????number2=number2+"8";
????????lable1.Content=number2;
??????}
????}
????privatevoidbutton9_Click(objectsender,RoutedEventArgse)
????{
??????if(flag==null)
??????{
????????number1=number1+"9";
????????lable1.Content=number1;
??????}
??????else
??????{
????????number2=number2+"9";
????????lable1.Content=number2;
??????}
????}
????privatevoidbuttonb_Click(objectsender,RoutedEventArgse)
????{
??????flag="-";
????}
????privatevoidbuttona_Click(objectsender,RoutedEventArgse)
????{
??????flag="+";
????}
????privatevoidbuttone_Click(objectsender,RoutedEventArgse)
????{
??????flag="%";
????}
????privatevoidbuttond_Click(objectsender,RoutedEventArgse)
????{
??????flag="/";
????}
????privatevoidbuttonc_Click(objectsender,RoutedEventArgse)
????{
??????flag="*";
????}
????privatevoidbuttoncc_Click(objectsender,RoutedEventArgse)
????{
??????this.lable1.Content="";
????}
????privatevoidbuttongg_Click(objectsender,RoutedEventArgse)
????{
??????if(flag==null)
??????{
????????number1=number1+".";
????????lable1.Content=number1;
??????}
??????else{
????????number2=number2+".";
????????lable1.Content=number2;
???????
??????}
????}
????privatevoidbuttonf_Click(objectsender,RoutedEventArgse)
????{
??????switch(flag){
????????case"+":lable1.Content=Convert.ToString(Convert.ToDouble(number1)+Convert.ToDouble(number2));
??????????break;
????????case"-":lable1.Content=Convert.ToString(Convert.ToDouble(number1)-Convert.ToDouble(number2));
??????????break;
????????case"*":lable1.Content=Convert.ToString(Convert.ToDouble(number1)*Convert.ToDouble(number2));
??????????break;
????????case"/":lable1.Content=Convert.ToString(Convert.ToDouble(number1)/Convert.ToDouble(number2));
??????????break;
????????case"%":lable1.Content=Convert.ToString(Convert.ToDouble(number1)%Convert.ToDouble(number2));
??????????break;
??????}
??????number1=null;
??????number2=null;
??????flag=null;
????}
??}
}
12:还有一种方法写!注意这个方法的是用textbox,不是用lable。
13:下面是这个的全部代码!
????xmlns="" ????xmlns:x="" ????Title="为什么我写的计算器这么多bug"Height="350"MaxHeight="350"MinHeight="350"Width="525"MaxWidth="525"MinWidth="525" ????> ?? ????? ???? ?????? ?????? ?????? ?????? ?????? ?????? ???? ???? ?????? ?????? ?????? ?????? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ????? ??
namespaceWpfApplication1
{
??///
??///MainWindow.xaml的交互逻辑
??///
??publicpartialclassMainWindow:Window
??{
????doublea;//定义为存储结果。
????doubleb;//定义为第一位操作符。
????boolc=false;
????stringd;//定义为操作运算符号。
????publicMainWindow左边()右边
????{
??????InitializeComponent左边()右边;
????}
????privatevoidbuttonCC_Click(objectsender,RoutedEventArgse)
????{
??????textbox1.Text="";
????}
????privatevoidbutton0_Click(objectsender,RoutedEventArgse)
????{
??????if(c==true){
????????textbox1.Text="";
????????c=false;
??????}
??????textbox1.Text+="0";
????}
????privatevoidbutton1_Click(objectsender,RoutedEventArgse)
????{
??????if(c==true){
????????textbox1.Text="";
????????c=false;
??????}
??????textbox1.Text+="1";
????}
????privatevoidbutton2_Click(objectsender,RoutedEventArgse)
????{
??????if(c==true)
??????{
????????textbox1.Text="";
????????c=false;
??????}
??????textbox1.Text+="2";
????}
????privatevoidbutton3_Click(objectsender,RoutedEventArgse)
????{
??????if(c==true)
??????{
????????textbox1.Text="";
????????c=false;
??????}
??????textbox1.Text+="3";
????}
????privatevoidbutton4_Click(objectsender,RoutedEventArgse)
????{
??????if(c==true)
??????{
????????textbox1.Text="";
????????c=false;
??????}
??????textbox1.Text+="4";
????}
????privatevoidbutton5_Click(objectsender,RoutedEventArgse)
????{
??????if(c==true)
??????{
????????textbox1.Text="";
????????c=false;
??????}
??????textbox1.Text+="5";
????}
????privatevoidbutton6_Click(objectsender,RoutedEventArgse)
????{
??????if(c==true)
??????{
????????textbox1.Text="";
????????c=false;
??????}
??????textbox1.Text+="6";
????}
????privatevoidbutton7_Click(objectsender,RoutedEventArgse)
????{
??????if(c==true)
??????{
????????textbox1.Text="";
????????c=false;
??????}
??????textbox1.Text+="7";
????}
????privatevoidbutton8_Click(objectsender,RoutedEventArgse)
????{
??????if(c==true)
??????{
????????textbox1.Text="";
????????c=false;
??????}
??????textbox1.Text+="8";
????}
????privatevoidbutton9_Click(objectsender,RoutedEventArgse)
????{
??????if(c==true)
??????{
????????textbox1.Text="";
????????c=false;
??????}
??????textbox1.Text+="9";
????}
????privatevoidbuttonc_Click(objectsender,RoutedEventArgse)
????{
??????c=true;
??????b=double.Parse(textbox1.Text);
??????d="*";
????}
????privatevoidbuttona_Click(objectsender,RoutedEventArgse)
????{
??????c=true;
??????b=double.Parse(textbox1.Text);
??????d="+";
????}
????privatevoidbuttonb_Click(objectsender,RoutedEventArgse)
????{
??????c=true;
??????b=double.Parse(textbox1.Text);
??????d="-";
????}
????privatevoidbuttond_Click(objectsender,RoutedEventArgse)
????{
??????c=true;
??????b=double.Parse(textbox1.Text);
??????d="/";
????}
????privatevoidbuttonf_Click(objectsender,RoutedEventArgse)
????{
??????c=true;
??????b=double.Parse(textbox1.Text);
??????d="%";
????}
????privatevoidbuttone_Click(objectsender,RoutedEventArgse)
????{
??????switch(d){
????????case"+":a=b+double.Parse(textbox1.Text);
??????????break;
????????case"-":a=b-double.Parse(textbox1.Text);
??????????break;
????????case"*":a=b*double.Parse(textbox1.Text);
??????????break;
????????case"/":a=b/double.Parse(textbox1.Text);
??????????break;
????????case"%":a=b%double.Parse(textbox1.Text);
??????????break;
??????}
??????textbox1.Text=a+"";
??????c=true;
????}
??}
}
根据大家的需求。以前写的计算器是不可以连续计算的。现在已经更改可以连续计算了。只是添加了几个判断。很简单的。
这个是代码,有注释哦。
第一:在计算器的数字进行判断。
第二:在等号的方法进行判断。超级简单易懂。
有需要源码的也可以联系我。有一次满足的大众的需求。哈哈哈哈哈~~~~
这个是项目的源码下载地:
如果有不足的地方欢迎多多指教!记得我是一个热爱编程的男孩子!有错误的地方多多指点!我是一个刚刚毕业入门的应届生!希望我们一起进步!有问题可以咨询我!欢迎给我留言!
- 错过应届生落户,这一份落户上海规划不能再错过
- 2022艺考生考上这些大学,毕业就有机会拿一线城市户口
- 2022上海落户政策再放宽!上财应届硕士毕业生可直接落户!
- “应届生”身份太珍贵!进央企国企考公端“铁饭碗”不要错过机会
- 落户上海很难吗?上海这两个批次的4所大学,毕业生可直接落户
- 博士享40万元购房资助!江苏这个县引才获顶级名校毕业生垂青
- 上海应届生落户
- 上海推进高校毕业生在沪就业 应届硕士生符合基本条件可直接落户
- 考上研究生竟然有这么多好处?这些研究生专属福利你必须知道
- 上海应届硕士可直接落户!各地研究生落户政策大盘点
- 2020应届毕业生有什么补贴,怎么领?_重复
- 2022上海应届生落户流程总结,从申请到办户口,看这篇就够了
- 2018年上海市选调应届优秀大学毕业生政策问答
- 考上大学=落户成功?你知道这些毕业就能拿户口的高校吗?_重复
- 2017上海大学应届生落户政策
- 2022上海应届生落户72分细则,上海落户政策2022最新规定
- 2020年上海应届生落户超全攻略
- 一文读懂!来沪人员随迁子女如何在沪继续就读高中阶段学校?
- 高校毕业季将至,浦东、闵行成为上海毕业生租住最热区域
- 2016应届毕业生考研报名时学习起止时间怎么写
- 【成人本科】2018年深圳积分入户成人高考本科能
- 2022山西阳泉市委党校引进急需紧缺岗位人才2人公告进入阅读模式
- 2022山东省济宁市能源局急需紧缺人才引进公告【14人】
- 2022山东省济宁市微山县大数据中心第二批急需紧缺人才引进公告【18人】
- 2022山东省济宁市微山县事业单位第三批急需紧缺青年优秀人才引进公告【17人】
- 2022山东济宁邹城引进人才公告
- 2022山东济宁市邹城市事业单位急需紧缺青年优秀人才(综合类)引进50人公告
- 2022山东济宁市微山县事业单位第三批急需紧缺青年优秀人才引进17人公告进入阅读模式
- 2022山东济宁市能源局急需紧缺人才引进14人公告进入阅读模式
- 儿童临时居住证怎么办
- 深圳安居房申请资格
- 大局已定:深圳高中城落户深汕合作区
- 深圳户籍儿童&非深户籍人员子女,入学所需资料
- 2022年深圳市全日制本科入户有多少积分
- 2022年深圳还可以走积分入户吗?这些你要知道
- 深圳积分入户,学历越高积分越高!你还不抓紧吗?
- 想2020年入深圳户口,你适合考哪个证书?
- 深圳社保代缴公司哪家好?
- 深圳积分入户的四个主要加分途径
- 深圳户口的四大搞钱方法!入户只为了搞钱
- 官方利好:猛抢海外人才,来就上海户口
- 上海降低落户门槛意味着排除后顾之忧
- 上海每个区的各有特色,选对地方早落户,落户更容易
- 留学生想要落户上海,入职前需要跟公司确认哪些事?
- 2022上海应届生积分入户条件查询入口,学生积分落户政策最新明确!
- 重磅!东莞2021年最新落户政策:申请流程+申请条件(建议收藏)
- 未来农村户口会有什么优势?
- 2022年上海应届生落户新规,这六所高校的应届生可以直接落户上海
- 居转户全流程展示
- 上海120积分,整理很多网友的具体问题,可能你也在其中!收藏