zoukankan      html  css  js  c++  java
  • 简易四则运算

    说明:
    在做完每道题后要按'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分析
    PSP Pensonal Time
    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)
     
  • 相关阅读:
    关于华为x1 7.0无法从eclipse发布的更新as发布的apk
    2015-10-29
    Android Exception18(Stuido debug .....)
    阿里巴巴校招运营专员笔试题
    业务性产品经理(商业领域)笔试题
    Android编程之常识
    2015-08-24
    What most young programmers need to learn
    hdu 1556 Color the ball
    跟着实例学习设计模式(3)-工厂方法(创建型)
  • 原文地址:https://www.cnblogs.com/snowz/p/4889653.html
Copyright © 2011-2022 走看看