典型使用者和情境代碼的實現

來源:互聯網
上載者:User

標籤:

<需求分析>

老陳希望通過一款軟體來儲存自己出的題,然後讓孩子自己去做並給出評價,

1,使用者可以輸入題。

2,可以儲存使用者輸入的題。

3,可以通過使用者輸入的題進行出題。

4,可以做出評價。

<設計思路>

1,建立windows表單,設計表單,並根據表單進行編碼。

2,首先建立儲存檔案夾。

3,寫一個儲存的方法。

4,然後是讀出使用者所出的題。

5,寫一個判斷正誤的方法。

<代碼實現>

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;using System.IO;namespace 出題{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        private void button1_Click(object sender, EventArgs e)//寫入        {                string fnm = @"one";                StreamWriter aaa = new StreamWriter(fnm,true);                aaa.WriteLine(this.textBox1.Text);                aaa.Close();                string fnmm = @"tow";                StreamWriter ddd = new StreamWriter(fnmm,true);                ddd.WriteLine(this.textBox2.Text);                ddd.Close();                string fnmn = @"fuhao";                StreamWriter fff = new StreamWriter(fnmn,true);                fff.WriteLine(this.comboBox1.Text);                fff.Close();                textBox1.Clear();                textBox2.Clear();                                         }        private void button2_Click(object sender, EventArgs e)        {            Form2 fam = new Form2();            fam.ShowDialog();                 }        private void button3_Click(object sender, EventArgs e)//清空題庫        {            string fnmm = @"tow";            StreamWriter ddd = new StreamWriter(fnmm);            ddd.WriteLine(" ");            ddd.Close();            string fnm = @"one";            StreamWriter aaa = new StreamWriter(fnm);            aaa.WriteLine("");            aaa.Close();            string fnmn = @"fuhao";            StreamWriter fff = new StreamWriter(fnmn);            fff.WriteLine("");            fff.Close();        }               }    }

 

 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;using System.IO;namespace 出題{    public partial class Form2 : Form    {        public Form2()        {            InitializeComponent();        }       private int sun;        private int i = 1;       public static int count;       public static int right;        private void timer1_Tick(object sender, EventArgs e)        {            try            {                int t = int.Parse(textBox5.Text);                if (t <= 0)                {                    timer1.Enabled = false;                    textBox5.Enabled = false;                    MessageBox.Show("時間到了!");                    Form3 fr3 = new Form3();                    fr3.ShowDialog();                }                t = t - 1;                textBox5.Text = t.ToString();            }            catch            {             }        }        private void button1_Click(object sender, EventArgs e)//開始        {            butt();            try            {                string t = textBox5.Text;                textBox5.Text = t;                timer1.Enabled = true;                timer1.Interval = 1000;                timer1.Start();            }            catch            {             }                         }        private void textBox4_KeyDown(object sender, KeyEventArgs e)        {                                  if (e.KeyCode == Keys.Enter)            {                                string f = textBox2.Text;                int a = int.Parse(textBox1.Text);                int b = int.Parse(textBox3.Text);                              calcula(f, a, b);                        if (textBox4.Text == sun.ToString())                {                    MessageBox.Show("回答正確!");                    right++;                }                else                {                    MessageBox.Show("回答錯誤!");                }                count++;                textBox4.Clear();                butt();                                                             }                                }//使用者的輸入        private void button2_Click(object sender, EventArgs e)//停止        {            textBox4.Enabled=false;        }        private  int calcula(string operato, int a, int b)        {            switch (operato)            {                case "+":                    sun = a + b;                    break;                case "-":                    sun = a - b;                    break;                case "*":                    sun = a * b;                    break;                case "/":                    sun = a / b;                    break;            }            return sun;                  }//求正確的值        private void butt()        {            string[] line = File.ReadAllLines("one");            if (i < line.Length )            {                textBox1.Text = line[i];                string[] lines = File.ReadAllLines("tow");                textBox3.Text = lines[i];                string[] lin = File.ReadAllLines("fuhao");                textBox2.Text = lin[i];            }            i++;            if (i == line.Length+1)            {                Form3 foo = new Form3();                foo.ShowDialog();            }                          }//讀題        private void button3_Click(object sender, EventArgs e)        {            this.Close();        }//關閉表單    }}

 form3的代碼

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 Form3 : Form    {        public Form3()        {            InitializeComponent();        }        private void button1_Click(object sender, EventArgs e)        {            this.Close();        }        private void Form3_Load(object sender, EventArgs e)        {            textBox1.Text = Form2.right.ToString();            textBox2.Text = (Form2.count - Form2.right).ToString();            textBox3.Text = ((Form2.right / (double)(Form2.count)) * 100).ToString() + "%";        }    }}

 <測試>

 <總結>

剛開始寫時真的沒有一點頭緒,不知道怎麼實現寫入,不用資料庫不知道怎麼儲存,就是寫入了也會清空上次的資料,終於不再清空上次的了,但是又不知道怎麼一個一個的讀出來,只能讀出一個,但是最後還是一個一個的實現了。感覺自己增長了不少,同時有感覺很有成就感。知道了代碼的嚴肅性,懂得了有時候看似不起眼的位置其實起了很大的作用。

 

典型使用者和情境代碼的實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.