标签:
需求分析:
编写一个能对0--10之间的整数(随机生成)进行四则运算的“软件”程序能接收用户输入的整数答案,并判断对错程序结束时,统计出答对、答错的题目数量。
设计思路:
我用的是Windows窗体应用程序,有3个Form窗体,如图:
在Form1中设置3个Textbox控件,表示随机数和输出结果,两个Label控件,代表运算符和“=”,7个Button控件代表“+、-、*、/、开始、说明、结束”;
Form2中4个Label控件,4个Textbox控件,表示“答题总数、正确数、错误数和正确率”;
Form3
代码:
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; namespace 四则运算 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } public static int Count = 0; public static int right = 0; private void button1_Click(object sender, EventArgs e) { RandomNum(); } private void RandomNum() { Random random = new Random(); int number1, number2; number1 = random.Next(1, 11); number2 = random.Next(1, 11); textBox1.Text = number1.ToString(); textBox2.Text = number2.ToString(); textBox3.Text = ""; Count++; } private void textBox3_KeyDown(object sender, KeyEventArgs e) { int sum; string n= label1.Text; switch (n) { 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.Enter) { if (textBox3.Text == sum.ToString()) right++; RandomNum(); } } private void button2_Click(object sender, EventArgs e) { textBox3.Enabled = true; Form2 frm2 = new Form2(); frm2.ShowDialog(); } private void button3_Click(object sender, EventArgs e) { label1.Text = "+"; } private void button4_Click(object sender, EventArgs e) { label1.Text = "-"; } private void button5_Click(object sender, EventArgs e) { label1.Text = "*"; } private void button6_Click(object sender, EventArgs e) { label1.Text = "/"; } private void button7_Click(object sender, EventArgs e) { new Form3().Show(); } } }
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; namespace 四则运算 { public partial class Form2 : Form { public Form2() { InitializeComponent(); } private void Form2_Load(object sender, EventArgs e) { textBox1.Text = Form1.Count.ToString(); textBox2.Text = Form1.right.ToString(); textBox3.Text = (Form1.Count - (double)(Form1.right)).ToString(); textBox4.Text = ((Form1.right / (double)(Form1.Count)) * 100).ToString() + "%"; } } }
运行结果:
PSP耗时分析:
总结:
这是第一次在不是照书抄的情况下自己设计程序,看到题目要求后就感觉自己不会做,后来发现以前的一次作业与这个类似,我就仿照了一些,但还是有一些地方不太明白,后请教他人解决了。但运行结果出来后发现因为程序自动产生下一道,总有一道是错的,不能解决,希望老师可以帮忙!
标签:
原文地址:http://www.cnblogs.com/chgcss/p/4857957.html