第三次作业

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

namespace Calculator
{
    public partial class Form1 : Form
    {
        public  int Count = 0;
        public  int right = 0;
        private int t = 60;

        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            comboBox1.SelectedIndex = 0;
        }
        private void RandomNum()
        {
            Random ran = new Random();
            int n1, n2;
            n1 = ran.Next(1, 11);
            n2 = ran.Next(1, 11);
            txtNum1.Text = n1.ToString();
            txtNum2.Text = n2.ToString();
            //if (comboBox1.Text == "-")
            switch (comboBox1.Text)
            {
                case "-":
                    while (true)
                    {
                        if (int.Parse(txtNum1.Text) >= int.Parse(txtNum2.Text))
                        {
                            break;
                        }
                        n1 = ran.Next(1, 11);
                        n2 = ran.Next(1, 11);
                        txtNum1.Text = n1.ToString();
                        txtNum2.Text = n2.ToString();
                    }
                    break;
                case "/":
                    break;
            }
            txtResult.Text = "";
            Count++;
        }

      

        private void btnStop_Click(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            txtResult.Enabled = false;
            MessageBox.Show("正确的题目数为:"+right+"错误的题目为:"+(Count-right-1));

        }

        private void btnStart_Click(object sender, EventArgs e)
        {
           
            try
            {
                t = int.Parse(txtTime.Text);
                if (t<=0)
                {
                    MessageBox.Show("时间不能为负数");
                    return;
                }
                label1.Text = t.ToString();
                txtTime.Enabled = false;
            }
            catch (Exception)
            {
                MessageBox.Show("请输入正确的时间");
                return;
            }
            txtResult.Enabled = true;
            timer1.Enabled = true;
            timer1.Interval = 1000;
            timer1.Start();
            RandomNum();
        }

        private void txtResult_KeyDown(object sender, KeyEventArgs e)
        {
            int result;
            if (e.KeyCode == Keys.Enter)
            {
                switch (comboBox1.Text)
                {
                    case "+":
                        result = int.Parse(txtNum1.Text) + int.Parse(txtNum2.Text);
                        break;
                    case "-":
                        result = int.Parse(txtNum1.Text) - int.Parse(txtNum2.Text);
                        break;
                    case "*":
                        result = int.Parse(txtNum1.Text) * int.Parse(txtNum2.Text);
                        break;
                    default  :
                        result = int.Parse(txtNum1.Text) / int.Parse(txtNum2.Text);
                        break;
                }
                if (txtResult.Text == Convert.ToString(result))
                {
                    right++;
 
                }
                RandomNum();

            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (t <= 0)
            {
                timer1.Enabled = false;
                txtResult.Enabled = false;
                MessageBox.Show("时间到!");
                //txtResult.Enabled = false;
                txtResult.Enabled = false;
                txtTime.Enabled = true;
                MessageBox.Show("正确的题目数为:" + right + "错误的题目为:" + (Count - right-1));
            }
            t = t - 1;
            label1.Text = t.ToString();
        }

      
    }

 

总结:通过这次作业我对程序对c#有了更深入的了解,加深了基础,实际上这次作业我的贡献不大,大部分是我的伙伴张峰完成。就在这过程中我依然收或不小,在看书查资料中巩固了学习的知识。我们一起解决遇到的各种问题,增长了知识也加深了友谊,还有完成代码之后的喜悦感,这次作业让我收获很多。

posted on 2015-10-18 15:40  想不起来了  阅读(182)  评论(1编辑  收藏  举报