天天看點

結對程式設計項目---四則運算

同組人:郝哲 黃雲龍

評價:在這次作業完成中我們都積極地查詢了有關資料,并向大神咨詢獲得了幫助,學習的過程中更更加深入的了解了C#的編寫。

感謝:感謝同組同學的幫助,感謝大神的輔導,感謝百度貼吧大神的講解。

//事件響應

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

namespace WindowsFormsApplication1

{

    public partial class Form1 : Form

    {  

        private int a=0,b=0;//資料

        private string op;//運算符

        private int result;///結果

        private Random rd = new Random();///随機數

        public Form1()

        {

            InitializeComponent();

        }

        private void buttonQeustion_Click(object sender, EventArgs e)

        { 

            int c = 0; 

            c=rd.Next(4);

            a=rd.Next(9);

            b = rd.Next(9);

           /*while (c!=0|c!=2){

                if (c == 1)

                {

                    c = rd.Next(4);

                }

                else if(b==0){

                    b = rd.Next(9);

            }*/

            switch(c){

                case 0: op = "+"; result = a + b; break;

                case 1: op = "-"; if (a >= b) //去除a-b小于0的情況

                                    {

                                        result = a - b;

                                    }

                                    else

                                        a += rd.Next(9,10);

                                    };break;

                case 2: op = "*"; result = a * b; break;

                case 3: op = "/"; if (b != 0)///分母為零的情況

                                    { 

                                        result = a / b;

                                    else 

                                        b = a;

                                    };  break;

            }

            labela.Text = a.ToString();

            labelb.Text = b.ToString();

            labelop.Text = op;

            textAnswer.Text = "";

        private void buttonJudge_Click(object sender, EventArgs e)

            string stringAnswer = textAnswer.Text;

            double resultAnswer = double.Parse(stringAnswer);

            string showStar = "" + a + op + b + "=" + stringAnswer;

            if (textAnswer.Text != "")

            {

                if (resultAnswer == result)

                    showStar += "       *";

                else

                    showStar += "       X";

            else { 

                 textAnswer.Text="答案欄目不能為空";

                 return;

            listBox1.Items.Add(showStar);

        private void buttonClose_Click(object sender, EventArgs e)

           Close();

    }

}

/////控件定義及其設定

    partial class Form1

    {

        /// <summary>

        /// 必需的設計器變量。

        /// </summary>

        private System.ComponentModel.IContainer components = null;

        /// 清理所有正在使用的資源。

        /// <param name="disposing">如果應釋放托管資源,為 true;否則為 false。</param>

        protected override void Dispose(bool disposing)

            if (disposing && (components != null))

                components.Dispose();

            base.Dispose(disposing);

        #region Windows 窗體設計器生成的代碼

        /// 設計器支援所需的方法 - 不要

        /// 使用代碼編輯器修改此方法的内容。

        private void InitializeComponent()

            this.labela = new System.Windows.Forms.Label();

            this.labelop = new System.Windows.Forms.Label();

            this.labelb = new System.Windows.Forms.Label();

            this.buttonQeustion = new System.Windows.Forms.Button();

            this.buttonJudge = new System.Windows.Forms.Button();

            this.listBox1 = new System.Windows.Forms.ListBox();

            this.buttonClose = new System.Windows.Forms.Button();

            this.textAnswer = new System.Windows.Forms.TextBox();

            this.equals = new System.Windows.Forms.Label();

            this.SuspendLayout();

            // 

            // labela

            this.labela.AutoSize = true;

            this.labela.Location = new System.Drawing.Point(40, 23);

            this.labela.Name = "labela";

            this.labela.Size = new System.Drawing.Size(0, 12);

            this.labela.TabIndex = 1;

            // labelop

            this.labelop.AutoSize = true;

            this.labelop.Location = new System.Drawing.Point(90, 25);

            this.labelop.Name = "labelop";

            this.labelop.Size = new System.Drawing.Size(0, 12);

            this.labelop.TabIndex = 2;

            // labelb

            this.labelb.AutoSize = true;

            this.labelb.Location = new System.Drawing.Point(139, 23);

            this.labelb.Name = "labelb";

            this.labelb.Size = new System.Drawing.Size(47, 12);

            this.labelb.TabIndex = 3;

            this.labelb.Text = "       ";

            // buttonQeustion

            this.buttonQeustion.Location = new System.Drawing.Point(38, 92);

            this.buttonQeustion.Name = "buttonQeustion";

            this.buttonQeustion.Size = new System.Drawing.Size(75, 23);

            this.buttonQeustion.TabIndex = 6;

            this.buttonQeustion.Text = "出題";

            this.buttonQeustion.UseVisualStyleBackColor = true;

            this.buttonQeustion.Click += new System.EventHandler(this.buttonQeustion_Click);

            // buttonJudge

            this.buttonJudge.Location = new System.Drawing.Point(154, 92);

            this.buttonJudge.Name = "buttonJudge";

            this.buttonJudge.Size = new System.Drawing.Size(75, 23);

            this.buttonJudge.TabIndex = 7;

            this.buttonJudge.Text = "判定";

            this.buttonJudge.UseVisualStyleBackColor = true;

            this.buttonJudge.Click += new System.EventHandler(this.buttonJudge_Click);

            // listBox1

            this.listBox1.FormattingEnabled = true;

            this.listBox1.ItemHeight = 12;

            this.listBox1.Location = new System.Drawing.Point(38, 137);

            this.listBox1.Name = "listBox1";

            this.listBox1.Size = new System.Drawing.Size(271, 112);

            this.listBox1.TabIndex = 8;

            // buttonClose

            this.buttonClose.Location = new System.Drawing.Point(276, 91);

            this.buttonClose.Name = "buttonClose";

            this.buttonClose.Size = new System.Drawing.Size(75, 23);

            this.buttonClose.TabIndex = 9;

            this.buttonClose.Text = "關閉";

            this.buttonClose.UseVisualStyleBackColor = true;

            this.buttonClose.Click += new System.EventHandler(this.buttonClose_Click);

            // textAnswer

            this.textAnswer.Location = new System.Drawing.Point(254, 19);

            this.textAnswer.Name = "textAnswer";

            this.textAnswer.Size = new System.Drawing.Size(100, 21);

            this.textAnswer.TabIndex = 10;

            // equals

            this.equals.AutoSize = true;

            this.equals.Location = new System.Drawing.Point(200, 25);

            this.equals.Name = "equals";

            this.equals.Size = new System.Drawing.Size(11, 12);

            this.equals.TabIndex = 11;

            this.equals.Text = "=";

            // Form1

            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);

            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

            this.ClientSize = new System.Drawing.Size(393, 261);

            this.Controls.Add(this.equals);

            this.Controls.Add(this.textAnswer);

            this.Controls.Add(this.buttonClose);

            this.Controls.Add(this.listBox1);

            this.Controls.Add(this.buttonJudge);

            this.Controls.Add(this.buttonQeustion);

            this.Controls.Add(this.labelb);

            this.Controls.Add(this.labelop);

            this.Controls.Add(this.labela);

            this.Name = "Form1";

            this.Text = "國小四則運算訓練";

            this.ResumeLayout(false);

            this.PerformLayout();

        #endregion

        private System.Windows.Forms.Label labela;

        private System.Windows.Forms.Label labelop;

        private System.Windows.Forms.Label labelb;

        private System.Windows.Forms.Button buttonQeustion;

        private System.Windows.Forms.Button buttonJudge;

        private System.Windows.Forms.ListBox listBox1;

        private System.Windows.Forms.Button buttonClose;

        private System.Windows.Forms.TextBox textAnswer;

        private System.Windows.Forms.Label equals;

//////主程式 

    static class Program

        /// 應用程式的主入口點。

        [STAThread]

        static void Main()

            Application.EnableVisualStyles();

            Application.SetCompatibleTextRenderingDefault(false);

            Application.Run(new Form1());

////運作效果圖