天天看點

四則運算

操作方法

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 = "-";

        } //button2事件  指派label1為減号。

        private void button3_Click(object sender, EventArgs e)

            label1.Text = "*";

        } //button3事件  指派label1為乘号。

        private void button4_Click(object sender, EventArgs e)

            label1.Text = "/";

        } //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

                    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++;

                        Count++;

                    textBox4.Text = Count.ToString();

                    textBox5.Text = right.ToString();

                    textBox6.Text = ((right / (double)Count) * 100).ToString() + "%";

                    jisuan();

            if (label1.Text == "*")

                int ji;

                ji = int.Parse(textBox1.Text) * int.Parse(textBox2.Text);

                    if (textBox3.Text == ji.ToString())

            if (label1.Text == "/")

                double chu;

                chu = int.Parse(textBox1.Text) / int.Parse(textBox2.Text);

                    if (double.Parse(textBox3.Text) == chu)

        } //textBox3_KeyDown事件

    }

}

思考題:

把計算方法中的取值範圍改為0到100就行了。