四则运算的程序

以上是第一张图片

以上是第二张图片

以上是第三张图片

以上是第四张图片

以上是第五张图片

以上是最后的图片。

做题思路:先建立了基本的框架,然后把计时器做好,再然后定义随机数和单击事件和让用户选择运算法,并让结果以窗口显示,最后检查程序。

这次代码用到了swift语句,共有3个窗体,Form1共有button控件7个,textBox控件3个,label控件4个。Form2主要是统计计算结果的有label控件3个,textBox控件3个。

Form3主要是说明这个程序的用途。计划10h完成,分析用了1h,设计框架0.5h,代码用了4h,检查和写总结用了1h,实际用6.5h。

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 _7._8
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public static int Count = 0;
        private int t = 60;
        public static int right = 0;
        private void button1_Click(object sender, EventArgs e)
        {
            label4.Text=t.ToString();
            timer1.Enabled = true;
            timer1.Interval = 1000;
            timer1.Start();
            RandomNum();
        }
        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 = "";
            Count++;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (t <= 0)
            {
                timer1.Enabled = false;
                textBox3.Enabled = false;
                MessageBox.Show("时间到!");
                textBox3.Enabled = false;
                Form2 frm = new Form2();
                frm.ShowDialog();
            }
            t = t - 1;
            label4.Text = t.ToString();
        }

        private void textBox3_KeyDown(object sender, KeyEventArgs e)
        {
            int text3;
            string a = label1.Text;
            switch (a)
            {
                case "+":
                    text3 = int.Parse(textBox1.Text) + int.Parse(textBox2.Text);
                    break;
                case "-":
                    text3 = int.Parse(textBox1.Text) - int.Parse(textBox2.Text);
                    break;
                case "*":
                    text3 = int.Parse(textBox1.Text) * int.Parse(textBox2.Text);
                    break;
                default:
                    text3 = int.Parse(textBox1.Text) / int.Parse(textBox2.Text);
                    break;
            }
         
            if (e.KeyCode == Keys.Enter)
            {
                if (textBox3.Text == text3.ToString())
                    right++;
                RandomNum();
            }
        }

        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)
        {
            new Form3().Show();
        }

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

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

        private void button6_Click(object sender, EventArgs e)
        {
            label1.Text = "*";
        }

        private void button7_Click(object sender, EventArgs e)
        {
            label1.Text = "/";
        }


    }
}

  最后是完整代码

posted @ 2015-10-01 00:14  刘远航  阅读(379)  评论(1编辑  收藏  举报