简易四则运算

说明:
在做完每道题后要按'Enter'键提交。如果自己选择用哪种计算类型,每次计算都要选择。随机运算也是每次计算都要选择一次。

Form1的代码:

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 四则运算
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public static int count = 0;//题目总数
        private int t = 60;         //测试时间为60秒
        public static int right = 0;//正确的题目总数
        public int sum;
        //产生 0-10 的随机数并显示在文本框中
        private void RandomNum()
        {
            Random ran = new Random();
            int n1, n2;
            n1 = ran.Next(0, 11);
            n2 = ran.Next(0, 11);
            textBox1.Text = Convert.ToString(n1);
            textBox3.Text = Convert.ToString(n2);
            textBox4.Text = "";
            count++;
        }
        //产生四则运算的随机符号并显示在文本框中
        private void RandomSun()
        {
            char[] chars=new char[]{'+','-','*','/'};
            Random ran = new Random();
            char n3;
            n3=chars[ran.Next(0,chars.Length)];
            textBox2.Text = Convert.ToString(n3);
        }
        //加法运算
        private void button1_Click(object sender, EventArgs e)
        {
            textBox4.Focus();
            RandomNum();
            sum = int.Parse(textBox1.Text) + int.Parse(textBox3.Text);
        }
        //减法运算
        private void button2_Click(object sender, EventArgs e)
        {
            textBox4.Focus();
            RandomNum();
            if (int.Parse(textBox1.Text) < int.Parse(textBox3.Text))//处理结果为负数的情况
            {
                sum = int.Parse(textBox3.Text) - int.Parse(textBox1.Text);
            }
            else
            {
                sum = int.Parse(textBox1.Text) - int.Parse(textBox3.Text);
            }
        }
        //乘法运算
        private void button3_Click(object sender, EventArgs e)
        {
            textBox4.Focus();
            RandomNum();
            sum = int.Parse(textBox1.Text) * int.Parse(textBox3.Text);
        }
        //除法运算
        private void button4_Click(object sender, EventArgs e)
        {
            textBox4.Focus();
            RandomNum();
            if (int.Parse(textBox3.Text) != 0)//处理分母为零的情况
            {
                sum =int.Parse(textBox1.Text) / int.Parse(textBox3.Text);
            }
            else
            {                
                RandomNum();
                count=count - 1;
            }
        }
        //开始的单击事件
        private void button6_Click(object sender, EventArgs e)
        {
            label2.Text = t.ToString();
            timer1.Enabled = true;
            timer1.Interval = 1000;
            timer1.Start();
            textBox4.Focus();
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (t <= 0)
            {
                timer1.Enabled = false;
                textBox4.Enabled = false;
                MessageBox.Show("时间!到!");
                textBox4.Enabled = false;
                Form2 frm = new Form2();
                frm.ShowDialog();
            }
            t = t - 1;
            label2.Text = t.ToString();
        }

        private void textBox4_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                    if (textBox4.Text == sum.ToString())
                        right++;
            }
        }
        //停止的单击事件
        private void button7_Click(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            textBox4.Enabled = false;
            Form2 frm = new Form2();
            frm.ShowDialog();
        }
        //随机算法的单击事件
        private void button5_Click(object sender, EventArgs e)
        {
            RandomSun();
            RandomNum();
            textBox4.Focus();
            string str = textBox2.Text.ToString();
            switch (str)
            {
                case "+":
                    sum = Convert.ToInt32(textBox1.Text) + Convert.ToInt32(textBox3.Text);
                    break;
                case "-":
                    if (int.Parse(textBox1.Text) < int.Parse(textBox3.Text))//处理结果为负数的情况
                    {
                        sum = Convert.ToInt32(textBox3.Text) - Convert.ToInt32(textBox1.Text);
                    }
                    sum = Convert.ToInt32(textBox1.Text) - Convert.ToInt32(textBox3.Text);
                    break;
                case "*":
                    sum = Convert.ToInt32(textBox1.Text) * Convert.ToInt32(textBox3.Text);
                    break;
                default:
                    if (int.Parse(textBox3.Text) != 0)//处理分母为零的情况
                    {
                        sum = int.Parse(textBox1.Text) / int.Parse(textBox3.Text);
                    }
                    else
                    {
                        RandomNum();
                        count = count - 1;
                    }
                    break;
            }           
        }        
    }
}

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 四则运算
{
    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.count - Form1.right).ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}
 
  

 总结:

做完这个程序,我发现有很多地方都可以再进一步简化,但是我不知道该怎么改,发现原来在这门语言上我才学习那么一点知识啊,是该多看看关于这方面的书了!

Psp分析
PSPPensonalTime
Planning计划30(m)
Estimate估计这个任务需要的时间是2(h)
Development开发3(h)
Analysis需求分析20(m)
Design  Review设计复审30(m)
Design具体设计20(m)
Coding具体编码2(h)
Code Review代码复审10(m)
Test测试10(m)
 

 

转载于:https://www.cnblogs.com/snowz/p/4889653.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值