码迷,mamicode.com
首页 > 其他好文 > 详细

四则运算

时间:2015-09-29 23:29:57      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:

操作方法

1.首先选择运算符号。

2.点击开始按钮。

3.运算结束后,点击结束按钮。

程序如图所示:

技术分享技术分享

运算代码如下:

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 jisuanji._1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private static int Count = 0, right = 0;    //定义总计和正确。
        private void jisuan()
        {
            Random rn = new Random();
            int a, b;                     
            a = rn.Next(11);
            b = rn.Next(1,11);
            textBox1.Text = a.ToString();
            textBox2.Text = b.ToString();
            textBox3.Text = "";

        }                    //算法的方法。
       
        private void button1_Click(object sender, EventArgs e)
        {
            label1.Text = "+";
            label1.Visible = true;
        }   //button1事件  赋值label1为加号。

        private void button2_Click(object sender, EventArgs e)
        {
            label1.Text = "-";
            label1.Visible = true;
        } //button2事件  赋值label1为减号。

        private void button3_Click(object sender, EventArgs e)
        {
            label1.Text = "*";
            label1.Visible = true;
        } //button3事件  赋值label1为乘号。

        private void button4_Click(object sender, EventArgs e)
        {
            label1.Text = "/";
            label1.Visible = true;
        } //button4事件  赋值label1为除号。

        private void button5_Click(object sender, EventArgs e)
        {
           
            jisuan();
        }//开始事件  引用算法。

        private void button6_Click(object sender, EventArgs e)
        {
            textBox3.Enabled = false;
            MessageBox.Show("测试结束!");
        }//结束事件 

        private void textBox3_KeyDown(object sender, KeyEventArgs e)
        {

            if (label1.Text == "+")//循环  如果label1为加
            {
                int sum;
                sum = int.Parse(textBox1.Text) + int.Parse(textBox2.Text);//计算机计算的值
                if (e.KeyCode == Keys.Enter) 
                {
                    if (textBox3.Text == sum.ToString())//计算机的值与用户输入的值作比较
                    {
                        right++;//正确值加1
                        Count++;//总计值加1

                    }
                    else
                        Count++;//总计值加1
                    textBox4.Text = Count.ToString();  //总计值赋值于textBox4
                    textBox4.Enabled = false;
                    textBox5.Text = right.ToString();   //正确值值赋值于textBox5
                    textBox5.Enabled = false;
                    textBox6.Text = ((right / (double)Count) * 100).ToString() + "%";//正确率总计值赋值于textBox6
                    textBox6.Enabled = false;
                    jisuan();//引用计算方法

 

 

                }

            }

 

 

解释同上
            else if (label1.Text == "-")
            {
                int cha;
                cha = int.Parse(textBox1.Text) - int.Parse(textBox2.Text);       
                if (e.KeyCode == Keys.Enter)
                {
                    if (textBox3.Text == cha.ToString())
                    {
                        right++; Count++;

                    }
                    else
                        Count++;
                    textBox4.Text = Count.ToString();
                    textBox4.Enabled = false;
                    textBox5.Text = right.ToString();
                    textBox5.Enabled = false;
                    textBox6.Text = ((right / (double)Count) * 100).ToString() + "%";
                    textBox6.Enabled = false;
                    jisuan();

 

 

                }

            }

 

 

解释同上
            if (label1.Text == "*")
            {
                int ji;
                ji = int.Parse(textBox1.Text) * int.Parse(textBox2.Text);
                if (e.KeyCode == Keys.Enter)
                {
                    if (textBox3.Text == ji.ToString())
                    {
                        right++; Count++;

                    }
                    else
                        Count++;
                    textBox4.Text = Count.ToString();
                    textBox4.Enabled = false;
                    textBox5.Text = right.ToString();
                    textBox5.Enabled = false;
                    textBox6.Text = ((right / (double)Count) * 100).ToString() + "%";
                    textBox6.Enabled = false;
                    jisuan();

 

 

                }

            }

 

 

解释同上
            if (label1.Text == "/")
            {
                double chu;
                chu = int.Parse(textBox1.Text) / int.Parse(textBox2.Text);
                if (e.KeyCode == Keys.Enter)
                {
                    if (double.Parse(textBox3.Text) == chu)
                    {
                        right++; Count++;

                    }
                    else
                        Count++;
                    textBox4.Text = Count.ToString();
                    textBox4.Enabled = false;
                    textBox5.Text = right.ToString();
                    textBox5.Enabled = false;
                    textBox6.Text = ((right / (double)Count) * 100).ToString() + "%";
                    textBox6.Enabled = false;
                    jisuan();

 


                }

            }

 

 


        } //textBox3_KeyDown事件
      
    }
}

思考题:

把计算方法中的取值范围改为0到100就行了。

 

四则运算

标签:

原文地址:http://www.cnblogs.com/herengdong666/p/4847395.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!