作业四:结对编程项目---四则运算
经过一段时间的学习和查阅资料,我跟小伙伴终于完成了这项任务,虽然过程磕磕绊绊,但最终还是把程序调试了出来。
下面是我的代码:(C#)
namespace _SZYS1 { 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(); } } }
我的界面截图 :