您好,欢迎访问三七文档
当前位置:首页 > IT计算机/网络 > C/C++资料 > 小学四则运算训练即简单实现(C#)
//事件响应usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespaceWindowsFormsApplication1{publicpartialclassForm1:Form{privateinta=0,b=0;//数据privatestringop;//运算符privateintresult;///结果privateRandomrd=newRandom();///随机数publicForm1(){InitializeComponent();}privatevoidbuttonQeustion_Click(objectsender,EventArgse){intc=0;c=rd.Next(4);a=rd.Next(9);b=rd.Next(9);/*while(c!=0|c!=2){if(c==1){c=rd.Next(4);}elseif(b==0){b=rd.Next(9);}}*/switch(c){case0:op=+;result=a+b;break;case1:op=-;if(a=b)//去除a-b小于0的情况{result=a-b;}else{a+=rd.Next(9,10);result=a-b;};break;case2:op=*;result=a*b;break;case3:op=/;if(b!=0)///分母为零的情况{result=a/b;}else{b=a;result=a/b;};break;}labela.Text=a.ToString();labelb.Text=b.ToString();labelop.Text=op;textAnswer.Text=;}privatevoidbuttonJudge_Click(objectsender,EventArgse){stringstringAnswer=textAnswer.Text;doubleresultAnswer=double.Parse(stringAnswer);stringshowStar=+a+op+b+=+stringAnswer;if(textAnswer.Text!=){if(resultAnswer==result){showStar+=*;}else{showStar+=X;}}else{textAnswer.Text=答案栏目不能为空;return;}listBox1.Items.Add(showStar);}privatevoidbuttonClose_Click(objectsender,EventArgse){Close();}}}/////控件定义及其设置namespaceWindowsFormsApplication1{partialclassForm1{///summary///必需的设计器变量。////summaryprivateSystem.ComponentModel.IContainercomponents=null;///summary///清理所有正在使用的资源。////summary///paramname=disposing如果应释放托管资源,为true;否则为false。/paramprotectedoverridevoidDispose(booldisposing){if(disposing&&(components!=null)){components.Dispose();}base.Dispose(disposing);}#regionWindows窗体设计器生成的代码///summary///设计器支持所需的方法-不要///使用代码编辑器修改此方法的内容。////summaryprivatevoidInitializeComponent(){this.labela=newSystem.Windows.Forms.Label();this.labelop=newSystem.Windows.Forms.Label();this.labelb=newSystem.Windows.Forms.Label();this.buttonQeustion=newSystem.Windows.Forms.Button();this.buttonJudge=newSystem.Windows.Forms.Button();this.listBox1=newSystem.Windows.Forms.ListBox();this.buttonClose=newSystem.Windows.Forms.Button();this.textAnswer=newSystem.Windows.Forms.TextBox();this.equals=newSystem.Windows.Forms.Label();this.SuspendLayout();////labela//this.labela.AutoSize=true;this.labela.Location=newSystem.Drawing.Point(40,23);this.labela.Name=labela;this.labela.Size=newSystem.Drawing.Size(0,12);this.labela.TabIndex=1;////labelop//this.labelop.AutoSize=true;this.labelop.Location=newSystem.Drawing.Point(90,25);this.labelop.Name=labelop;this.labelop.Size=newSystem.Drawing.Size(0,12);this.labelop.TabIndex=2;////labelb//this.labelb.AutoSize=true;this.labelb.Location=newSystem.Drawing.Point(139,23);this.labelb.Name=labelb;this.labelb.Size=newSystem.Drawing.Size(47,12);this.labelb.TabIndex=3;this.labelb.Text=;////buttonQeustion//this.buttonQeustion.Location=newSystem.Drawing.Point(38,92);this.buttonQeustion.Name=buttonQeustion;this.buttonQeustion.Size=newSystem.Drawing.Size(75,23);this.buttonQeustion.TabIndex=6;this.buttonQeustion.Text=出题;this.buttonQeustion.UseVisualStyleBackColor=true;this.buttonQeustion.Click+=newSystem.EventHandler(this.buttonQeustion_Click);////buttonJudge//this.buttonJudge.Location=newSystem.Drawing.Point(154,92);this.buttonJudge.Name=buttonJudge;this.buttonJudge.Size=newSystem.Drawing.Size(75,23);this.buttonJudge.TabIndex=7;this.buttonJudge.Text=判定;this.buttonJudge.UseVisualStyleBackColor=true;this.buttonJudge.Click+=newSystem.EventHandler(this.buttonJudge_Click);////listBox1//this.listBox1.FormattingEnabled=true;this.listBox1.ItemHeight=12;this.listBox1.Location=newSystem.Drawing.Point(38,137);this.listBox1.Name=listBox1;this.listBox1.Size=newSystem.Drawing.Size(271,112);this.listBox1.TabIndex=8;////buttonClose//this.buttonClose.Location=newSystem.Drawing.Point(276,91);this.buttonClose.Name=buttonClose;this.buttonClose.Size=newSystem.Drawing.Size(75,23);this.buttonClose.TabIndex=9;this.buttonClose.Text=关闭;this.buttonClose.UseVisualStyleBackColor=true;this.buttonClose.Click+=newSystem.EventHandler(this.buttonClose_Click);////textAnswer//this.textAnswer.Location=newSystem.Drawing.Point(254,19);this.textAnswer.Name=textAnswer;this.textAnswer.Size=newSystem.Drawing.Size(100,21);this.textAnswer.TabIndex=10;////equals//this.equals.AutoSize=true;this.equals.Location=newSystem.Drawing.Point(200,25);this.equals.Name=equals;this.equals.Size=newSystem.Drawing.Size(11,12);this.equals.TabIndex=11;this.equals.Text==;////Form1//this.AutoScaleDimensions=newSystem.Drawing.SizeF(6F,12F);this.AutoScaleMode=System.Windows.Forms.AutoScaleMode.Font;this.ClientSize=newSystem.Drawing.Size(393,261);this.Controls.Add(this.equals);this.Controls.Add(this.textAnswer);this.Controls.Add(this.buttonClose);this.Controls.Add(this.listBox1);this.Controls.Add(this.buttonJudge);this.Controls.Add(this.buttonQeustion);this.Controls.Add(this.labelb);this.Controls.Add(this.labelop);this.Controls.Add(this.labela);this.Name=Form1;this.Text=小学四则运算训练;this.ResumeLayout(false);this.PerformLayout();}
本文标题:小学四则运算训练即简单实现(C#)
链接地址:https://www.777doc.com/doc-7377484 .html