计算机四则运算

一、需求分析:

1、当单击开始的时候,开始进行运算,单击回车键数字可以随机变换数字,单击停止跳转到Form2进行测试。

2、程序随机产生两个1-10的随机数,由用户输入结果。

3、停止时会输出正确率信息。

二、设计思路:

1、先建两个窗体,拉进去自己需要的控件,然后编写代码。

2、定义正确和总的题数的变量。

3、Random产生随机数。

4、textBox3计算加减乘除结果,单击开始运行。

5、停止转到Form2进行测试。 

具体控件如下表

控件属性
Form1Text加法测试
label1Text+
label2Text=
textBox1Enbledfalse
textBox2Enbledfalse
button1Text开始
button2Text停止
button3Text+
button4Text-
button5Text*
button6Text/
textBox3Enbledfalse

Form2

 

  
label1Text总计:
label2Text正确:
label3Text正确率:
textBox1Enbledfalse
textBox2Enbledfalse
textBox3Enbledfalse

三、代码实现:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

 

namespace Chapter7_8

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

 

        private void Form1_Load(object sender, EventArgs e)

        {

 

        }

 

        public static int count = 0;//题目总数

        public static int right = 0;//正确的题目总数

        private void button1_Click(object sender, EventArgs e)

        {

          

            Random();

 

        }

//产生1-10的随机数

        private void Random()

        {

            Random ran = new Random();

            int n1, n2;

            n1 = ran.Next(1, 11);

            n2 = ran.Next(1, 11);

            textBox1.Text = n1.ToString();

            textBox2.Text = n2.ToString();

            textBox3.Text="";

            count++;

 

         }

//当按下回车键表示输入结果

        private void textBox3_KeyDown(object sender, KeyEventArgs e)

        {

            int count;

            string c = label2.Text;

            switch (c)

            {

                case "+":

                    count = int.Parse(textBox1.Text) + int.Parse(textBox2.Text);

                    break;

                case "-":

                    count = int.Parse(textBox1.Text) - int.Parse(textBox2.Text);

                    break;

                case "x":

                    count = int.Parse(textBox1.Text) * int.Parse(textBox2.Text);

                    break;

                default:

                    count = int.Parse(textBox1.Text) / int.Parse(textBox2.Text);

                    break;

                  }

                    if (e.KeyCode == Keys.Enter)

                    {

                        if (textBox3.Text == count.ToString())

                            right++;

                        Random();

 

                    }

            }

 

        private void button2_Click(object sender, EventArgs e)

        {

            textBox3.Enabled = false;

            Form2 frm2 = new Form2();

            frm2.ShowDialog();

        }

 

        private void button3_Click(object sender, EventArgs e)

        {

            label2.Text = "+";

            Random();

        }

 

        private void button4_Click(object sender, EventArgs e)

        {

            label2.Text = "-";

            Random();

        }

 

        private void button5_Click(object sender, EventArgs e)

        {

            label2.Text =" *";

            Random();

        }

 

        private void button6_Click(object sender, EventArgs e)

        {

            label2.Text = "/";

            Random();

        }

        }

        

    }

 

//Form2窗体代码编写

 

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Chapter7_8
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}

private void Form2_Load(object sender, EventArgs e)
{
textBox1.Text = Form1.count.ToString();
textBox2.Text = Form1.right.ToString();
textBox3.Text = ((Form1.right / (double)(Form1.count)) * 100).ToString() + "%";
}

四、测试

       

五、psp耗时分析:

pspPersonal Software Process StagesTime(h)
planning.计划8
.Estimate.估计这个任务需要多长时间6
Development.开发70
.Analysis.需求分析5
.Design Spec.生成设计文档6
.Design Review.设计复审4
• Coding Standard.代码规范2
 • Design.具体设计10
 • Coding.具体编码15
• Code Review.代码复审8
 • Text.测试22
Reporting.报告6
• Test Report.测试报告3
• Size Measurement.计算工作量0.5
 • Postmortem&Process Improvement Plan.事后总结并提出改进计划4

六、总结:

       在写代码的时候自己犯了可多错误,本来自己对编程学的都不太好,现在让编代码真的有点难度,不过我是看老师的例子做的,我们学的跟这可相似,自己底子不好就稍微改了一下,我一定要多努力自己写出来代码,而不是现在改我们学过的例子的代码,总的来说,这次四则运算不是太难,只要我们平常多用点心都能做出来。本来是写控制台的,不过控制台正确率总是出不来,,所以只好用窗体了,相对来说窗体简单点。希望自己下次做的更好,加油!!!

思考题

只要把

   private void Random()

 

        {

 

            Random ran = new Random();

 

            int n1, n2;

 

            n1 = ran.Next(1, 11);

 

            n2 = ran.Next(1, 11);

 

            textBox1.Text = n1.ToString();

 

            textBox2.Text = n2.ToString();

 

            textBox3.Text="";

 

            count++;

//换成

 

   private void Random()

 

        {

 

            Random ran = new Random();

 

            int n1, n2;

 

            n1 = ran.Next(1, 101);

 

            n2 = ran.Next(1, 101);

 

            textBox1.Text = n1.ToString();

 

            textBox2.Text = n2.ToString();

 

            textBox3.Text="";

 

            count++;//就行了

 

转载于:https://www.cnblogs.com/caixiufang/p/4853425.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值