環境:C#窗體應用程序.git
功能需求:github
響應: 字母鍵1234選擇加減乘除, enter鍵結束程序顯示統計結果.dom
輸入數據: 隨機1~10整數, 接收整數答案.
測試
輸出數據: 運算結果, 判斷對錯, 統計答對答錯數量.spa
Form1(運算窗體)設計
產生1~10的random整數並在兩個文本框中顯示3d
字母鍵qwer對應運算符+-×÷code
判斷用戶輸入的結果是否正確,right++||wrong++orm
按下結束(enter)後調用Form2.blog
Form2(統計結果)
顯示答對的數量&顯示答錯的數量
namespace Elementary_Arithmetic { public partial class Form1 : Form { public Form1() { InitializeComponent(); } public static int right = 0; public static int wrong = 0; private void RandomNum() { Random ran = new Random(); int n1, n2; n1 = ran.Next(1, 11); n2 = ran.Next(1, 11); textBox1.Text = n1.ToString(); textBox2.Text = n2.ToString(); textBox3.Text = ""; } private void button1_Click(object sender, EventArgs e) { label4.Text = "+"; } private void button2_Click(object sender, EventArgs e) { label4.Text = "-"; } private void button3_Click(object sender, EventArgs e) { label4.Text = "×"; } private void button4_Click(object sender, EventArgs e) { label4.Text = "÷"; } private void button5_Click(object sender, EventArgs e) { int math; if (label4.Text == "+") math = int.Parse(textBox1.Text) + int.Parse(textBox2.Text); else if (label4.Text == "-") math = int.Parse(textBox1.Text) - int.Parse(textBox2.Text); else if (label4.Text == "×") math = int.Parse(textBox1.Text) * int.Parse(textBox2.Text); else math = int.Parse(textBox1.Text) / int.Parse(textBox2.Text); if (textBox3.Text == math.ToString()) right++; else wrong++; RandomNum(); } private void Form1_Load(object sender, EventArgs e) { RandomNum(); } private void button6_Click(object sender, EventArgs e) { Form2 frm2 = new Form2(); frm2.ShowDialog(); } private void button6_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { Form2 frm2 = new Form2(); frm2.ShowDialog(); } } private void button1_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Q) { label4.Text = "+"; } } private void button2_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.W) { label4.Text = "-"; } } private void button3_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.E) { label4.Text = "×"; } } private void button4_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.R) { label4.Text = "÷"; } } } }
namespace Elementary_Arithmetic { public partial class Form2 : Form { public Form2() { InitializeComponent(); } private void Form2_Load(object sender, EventArgs e) { textBox1.Text = Form1.right.ToString(); textBox2.Text = Form1.wrong.ToString(); } } }
測試的時候發現文本框的焦點和響應按鍵有衝突,影響不大,沒有發現別的問題.
此次做業,我盡力了,加油!
https://github.com/4Aiur/4arithmetic