需求分析:用户能使用此程序运行1-10之内的四则运算,具有暂停和统计正确率的功能。
设计思路:设计一个可以四则运算的程序,用控制台或者Windows窗体都可实现。我使用的是windows窗体。
代码如下
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 Form1 : Form { public Form1() { InitializeComponent(); } public static int Count = 0; private int t = 60; public static int right = 0; private void button5_Click(object sender, EventArgs e) { label5.Text = t.ToString(); timer1.Enabled = true; timer1.Interval = 1000; timer1.Start(); RandomNum(); } private void RandomNum() { Random random = new Random(); int n1, n2; n1 = random.Next(1, 11); n2 = random.Next(1, 11); textBox1.Text = n1.ToString(); textBox2.Text = n2.ToString(); textBox3.Text = ""; Count++; } private void textBox3_KeyDown(object sender, KeyEventArgs e) { int sum; string x = label1.Text; switch (x) { case "+": sum = int.Parse(textBox1.Text) + int.Parse(textBox2.Text); break; case "-": sum = int.Parse(textBox1.Text) - int.Parse(textBox2.Text); break; case "*": sum = int.Parse(textBox1.Text) * int.Parse(textBox2.Text); break; default: sum = int.Parse(textBox1.Text) / int.Parse(textBox2.Text); break; } if (e.KeyCode == Keys.Space) { if (textBox3.Text == sum.ToString()) right++; RandomNum(); } } private void button6_Click(object sender, EventArgs e) { textBox3.Enabled =true; Form2 frm2 = new Form2(); frm2.ShowDialog(); timer1.Stop(); } private void button1_Click(object sender, EventArgs e) { label1.Text = "+"; } private void button2_Click(object sender, EventArgs e) { label1.Text = "-"; } private void button3_Click(object sender, EventArgs e) { label1.Text = "*"; } private void button4_Click(object sender, EventArgs e) { label1.Text = "/"; } private void timer1_Tick_1(object sender, EventArgs e) { if (t <= 0) { timer1.Enabled = false; textBox3.Enabled = false; MessageBox.Show("时间到!"); textBox3.Enabled = false; Form2 frm2 = new Form2(); frm2.ShowDialog(); } t = t - 1; label5.Text = t.ToString(); } } }
总结:我问了寝室的同学好长时间才做了出来,如果有不足的地方,请老师教导。
以后这些作业尽量自己完成,会进步很大。
原文:http://www.cnblogs.com/ahr1/p/4857618.html