环境: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