一、内容
用Windows窗体应用程序,实现1-10以内的加减乘除测试程序。
二、设计过程
a、创建Window窗体应用程序,根据需要在添加一个窗体。
b、根据所需对窗体进行设计。图如下:


c、设计好窗体后,对窗体各控件属性设置。
d、为了使其程序产生随机数,用户能通过点击加减乘除和输入结果,而且在点击结束时,程序会给出正确和错误个数,及共答题数,对窗体进行编写代码。
e、运行结果如下:




具体代码如下:
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 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 = "";
Count++;
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
label4.Text = "+";
RandomNum();
}
private void button2_Click(object sender, EventArgs e)
{
label4.Text = "-";
RandomNum();
}
private void button3_Click(object sender, EventArgs e)
{
label4.Text = "×";
RandomNum();
}
private void button4_Click(object sender, EventArgs e)
{
label4.Text = "÷";
RandomNum();
}
private void textBox3_KeyDown(object sender, KeyEventArgs e)
{
int sum;
string str = label4.Text;
switch (str)
{
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 (e.KeyCode == Keys.Enter)
{
if (textBox3.Text == sum.ToString())
right++;
RandomNum();
}
}
}
private void button5_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.ShowDialog();
}
}
}
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 运算结果_Load(object sender, EventArgs e)
{
textBox1.Text = Form1.Count.ToString();
textBox2.Text = Form1.right.ToString();
textBox3.Text = ((Form1.right / (double)(Form1.Count)) * 100).ToString() + "%";
}
}
}
f、PSP耗时:为了做好这个程序,查资料,翻阅书大概用了1个多小时,完成编码大概用时1h!
个人总结:
我感觉有点难度,看起来很简单,做起来很麻烦,书上有类似的,但我做这个程序耗时挺多的,不管怎么样,我是很努力做了,希望大家呢,能有耐心,耗时长不是事,坚持 就是胜利。
做这个程序方法很多,不管是简单的,还是复杂的方法,主要是摸清门道,对以后学习很有帮助,最主要的是要有很大的耐心。
原文:http://www.cnblogs.com/64gong/p/4852918.html