標籤:
<需求分析>
老陳希望通過一款軟體來儲存自己出的題,然後讓孩子自己去做並給出評價,
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() + "%"; } }}
<測試>
<總結>
剛開始寫時真的沒有一點頭緒,不知道怎麼實現寫入,不用資料庫不知道怎麼儲存,就是寫入了也會清空上次的資料,終於不再清空上次的了,但是又不知道怎麼一個一個的讀出來,只能讀出一個,但是最後還是一個一個的實現了。感覺自己增長了不少,同時有感覺很有成就感。知道了代碼的嚴肅性,懂得了有時候看似不起眼的位置其實起了很大的作用。
典型使用者和情境代碼的實現