进行十以内的四则运算

1.需求分析

  环境:C#窗体应用程序.

  功能需求:

    响应:    字母键1234选择加减乘除,  enter键结束程序显示统计结果.

    输入数据:  随机1~10整数,  接收整数答案.

    输出数据:  运算结果,  判断对错, 统计答对答错数量.

2.具体设计思路

  Form1(运算窗体)

    产生1~10的random整数并在两个文本框中显示

    字母键qwer对应运算符+-×÷

    判断用户输入的结果是否正确,right++||wrong++

        按下结束(enter)后调用Form2.

  Form2(统计结果)

    显示答对的数量&显示答错的数量

3.代码实现

namespace Elementary_Arithmetic
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public static int right = 0;
        public static int wrong = 0;
        private void RandomNum()
        {
            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 = "";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            label4.Text = "+";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            label4.Text = "-";
        }

        private void button3_Click(object sender, EventArgs e)
        {
            label4.Text = "×";
        }

        private void button4_Click(object sender, EventArgs e)
        {
            label4.Text = "÷";
        }

        private void button5_Click(object sender, EventArgs e)
        {
            int math;
            if (label4.Text == "+")
                math = int.Parse(textBox1.Text) + int.Parse(textBox2.Text);
            else if (label4.Text == "-")
                math = int.Parse(textBox1.Text) - int.Parse(textBox2.Text);
            else if (label4.Text == "×")
                math = int.Parse(textBox1.Text) * int.Parse(textBox2.Text);
            else math = int.Parse(textBox1.Text) / int.Parse(textBox2.Text);
            if (textBox3.Text == math.ToString())
                right++;
            else wrong++;
            RandomNum();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            RandomNum();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            Form2 frm2 = new Form2();
            frm2.ShowDialog();
        }

        private void button6_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Form2 frm2 = new Form2();
                frm2.ShowDialog();
            }
        }

        private void button1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Q)
            {
                label4.Text = "+";
            }
        }

        private void button2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.W)
            {
                label4.Text = "-";
            }
        }

        private void button3_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.E)
            {
                label4.Text = "×";
            }
        }

        private void button4_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.R)
            {
                label4.Text = "÷";
            }
        }
    }
}
namespace Elementary_Arithmetic
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            textBox1.Text = Form1.right.ToString();
            textBox2.Text = Form1.wrong.ToString();
        }
    }
}

 

4.测试

 

5.PSP耗时分析

 

6.总结

测试的时候发现文本框的焦点和响应按键有冲突,影响不大,没有发现别的问题.

这次作业,我尽力了,加油!

7.Github托管

https://github.com/4Aiur/4arithmetic

 

转载于:https://www.cnblogs.com/likephenix/p/4855726.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值