程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#窗體隨機四則運算 (第四次作業),

C#窗體隨機四則運算 (第四次作業),

編輯:C#入門知識

C#窗體隨機四則運算 (第四次作業),


---恢復內容開始---

增量內容:1)處理用戶的錯誤輸入,比如輸入字母或符號等,處理除法運算中分母為0的情況,處理結果為負數的情況,保證是小學水平不出現負數,比如不能出現5-8=-3這種情況;2)用戶可以設定倒計時;  3)用戶可以設定隨機整數的范圍和題目數量;4)用戶可以選擇哪種計算類型,比如加減乘除,或可選擇軟件隨機生成四則運算中的一種;  5)用戶可以選擇隨機生成的題目中是否帶有小括號,比如(2+3)*5,如果是gui程序,添加這個功能可以用復選框實現;    6)保證生成過的題目不再重復出現。

設計思路:上次作業我們用的是控制台,這次原本還想用控制台,但是我的隊友洪亮建議我用窗體,所以這次做這道題完全是把開始的作業都做了一遍,開始的時候其實還蠻順利的,知道要實現用戶可以輸入范圍的時候遇到了一些小問題,但是很快就解決了,完成其它實現的時候我們也會有一些分歧,不如要不要做個提示告訴用戶在同一個界面上出現你已經做了多少道題,我說不要,他要,但是還是寫上了,但是等到界面全部完成後,感覺加上還蠻合適的,就這樣我們每個人負責一些任務,遇到問題我們就在一起解決,然而比一個

人做輕松了好多。下面是我們結對時的照片!!!

需求分析:增加的內容前幾個還好啦,不是很難,但是要實現混合運算有點想不到方法,我們也上查了一下,但是網上介紹的方法比如棧,樹的方式不是很懂,我們也實驗了好多可是都沒成功,有好多疑問,因為時間比較緊,所以課後做好了。

namespace _000000
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public static int Count = 0;
        public static int right = 0;
        public static int fault = 0;  
private void RandomNum() { Random ran = new Random(); int a, b; string m; a = ran.Next(int.Parse(textBox4.Text), int.Parse(textBox5.Text)); b = ran.Next(int.Parse(textBox4.Text), int.Parse(textBox5.Text)); if (a >= b) { textBox1.Text = a.ToString(); textBox2.Text = b.ToString(); textBox3.Text = ""; Count++; textBox6.Text = Form1.Count.ToString(); if (textBox7.Text == textBox6.Text) { MessageBox.Show("你已做完計算!"); } } else if (a < b) { m = b.ToString(); textBox2.Text = a.ToString(); textBox1.Text = m; textBox3.Text = ""; Count++; textBox6.Text = Form1.Count.ToString(); if (textBox7.Text == textBox6.Text) { MessageBox.Show("你已做完計算!"); } } } private void timer1_Tick(object sender, EventArgs e) { if (Convert.ToInt32(textBox8.Text) <= 0) {

                  timer1.Enabled = false;
                  textBox3.Enabled = false;
                  MessageBox.Show("時間到!");
                  textBox3.Enabled = false;
                  Form2 frm2 = new Form2();
                  frm2.ShowDialog()

            }

              int t = Convert.ToInt32(textBox8.Text);

              t = t - 1;

              textBox8.Text = t.ToString();

        }

private void textBox3_KeyDown(object sender, KeyEventArgs e)
        {
            int sum = 0;
            string m = label3.Text;

            if (m == "+")
            {

                sum = int.Parse(textBox1.Text) + int.Parse(textBox2.Text);
                if (e.KeyCode == Keys.Enter)
                {
                    if (textBox3.Text == sum.ToString())
                    {
                        right++;

                        RandomNum();
                    }
                    else
                    {
                        fault++;
                        RandomNum();
                    }
                }
            }

            else if (m == "-")
            {
                sum = int.Parse(textBox1.Text) - int.Parse(textBox2.Text);
                if (e.KeyCode == Keys.Enter)
                {
                    if (textBox3.Text == sum.ToString())
                    {
                        right++;

                        RandomNum();
                    }
                    else
                    {
                        fault++;
                        RandomNum();
                    }
                }
            }



            else if (m == "x")
            {
                sum = int.Parse(textBox1.Text) * int.Parse(textBox2.Text);
                if (e.KeyCode == Keys.Enter)
                {
                    if (textBox3.Text == sum.ToString())
                    {
                        right++;

                        RandomNum();
                    }
                    else
                    { fault++;
                    RandomNum();
                    }
                }
            }


            else
            {
                sum = int.Parse(textBox1.Text) / int.Parse(textBox2.Text);

            }
            
        }
        private void button1_Click(object sender, EventArgs e)
        {
            label3.Text = "+";
int t = Convert.ToInt32(textBox8.Text); label2.Text = t.ToString(); timer1.Enabled = true; timer1.Interval = 1000; timer1.Start(); RandomNum(); } private void button2_Click(object sender, EventArgs e) { label3.Text = "-";
int t = Convert.ToInt32(textBox8.Text); label2.Text = t.ToString(); timer1.Enabled = true; timer1.Interval = 1000; timer1.Start(); RandomNum(); } private void button3_Click(object sender, EventArgs e) { label3.Text = "x";
int t = Convert.ToInt32(textBox8.Text); label2.Text = t.ToString(); timer1.Enabled = true; timer1.Interval = 1000; timer1.Start(); RandomNum(); } private void button4_Click(object sender, EventArgs e) { label3.Text = "/";
int t = Convert.ToInt32(textBox8.Text); label2.Text = t.ToString(); timer1.Enabled = true; timer1.Interval = 1000; timer1.Start(); Random ran = new Random(); int a, b; string m; a = ran.Next(int.Parse(textBox4.Text), int.Parse(textBox5.Text)); b = ran.Next(int.Parse(textBox4.Text), int.Parse(textBox5.Text)); if (b != 0) { textBox1.Text = a.ToString(); textBox2.Text = b.ToString(); textBox3.Text = ""; Count++; textBox6.Text = Form1.Count.ToString(); if (textBox7.Text == textBox6.Text) { MessageBox.Show("你已做完計算!"); } } else { m = b.ToString(); textBox2.Text = a.ToString(); textBox1.Text = m; textBox3.Text = ""; Count++; textBox6.Text = Form1.Count.ToString(); if (textBox7.Text == textBox6.Text) { MessageBox.Show("你已做完計算!"); } } } private void button7_Click(object sender, EventArgs e) { Form2 frm2 = new Form2(); frm2.ShowDialog(); } private void textBox4_TextChanged(object sender, EventArgs e) { int result; if (int.TryParse(textBox4.Text, out result) == false) { if (!(textBox4.Text == "")) { MessageBox.Show("請輸入數字!", "提示"); textBox4.Clear(); } } } private void textBox5_TextChanged(object sender, EventArgs e) { int result; if (int.TryParse(textBox5.Text, out result) == false) { if (!(textBox5.Text == "")) { MessageBox.Show("請輸入數字!", "提示"); textBox5.Clear(); } } } private void textBox7_TextChanged(object sender, EventArgs e) { int result; if (int.TryParse(textBox7.Text, out result) ==false) { if (!(textBox7.Text == "")) { MessageBox.Show("請輸入數字!", "提示"); textBox7.Clear(); } } } private void button5_Click(object sender, EventArgs e) { textBox1.Clear(); textBox2.Clear(); textBox3.Clear(); textBox4.Clear(); textBox5.Clear(); textBox7.Clear(); } } }

 

PsP耗時

總結:沒到看到和自己的伙伴完成一個項目時,頓時感覺付出的辛苦和努力都很值得,特別是看到隊友那嘴角上揚的笑容,他心裡應該會很自豪吧,但我也會感到非常快樂。結對編程是第二次做,比起第一次感覺配合的更默契一點,希望以後還是結對編程。

最後有一個小小的請求,希望老師能提示一下實現混合運算的方法如果老師能有個例子就更好了!!!

 

 

 

 


             
           
             

           

         

---恢復內容結束---

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved