第三次作业
一 、需求分析
当用户单击开始按钮时程序会自动产生两个随机的数,并且所产生的两个数第一个数一定大于第二个数不会出现出现负数的情况;
用户可以自己选择时间,时间以秒为单位,时间到了textBox4将不能再输入答案同时Form2也会弹出,统计出结果;
用户也可以选择计算的总题数,题数做完Form2也会弹出,统计出结果;
用户不想做时可以单击停止直接统计出结果。
二、设计思路
先设计Form1、Form2 并编写代码
Form1中先获取两个随机数、算法、定时器、计算的总题数
然后Form2中再编写
三、插入代码
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;
public static int a;
public static int b;
public static int t;
public static int n1;
public static int n2;
private void button2_Click(object sender, EventArgs e)
{
t = int.Parse(textBox5.Text);
textBox5.Text = t.ToString();
timer1.Enabled = true;
timer1.Interval = 1000;
timer1.Start();
an();
}
private void button3_Click(object sender, EventArgs e)//计算的方法
{
string z = textBox3.Text;
string s = textBox4.Text;
int q = int.Parse(s);
if (z == "+")
{
if (q == a + b)
{
right++;
}
}
if (z == "-")
{
if (q == a - b)
{
right++;
}
}
if (z == "*")
{
if (q == a * b)
{
right++;
}
}
if (z == "/")
{
if (q == a / b)
{
right++;
}
}
an();
}
private void an()//随机产生两个数
{
Random shu=new Random();
n1=int.Parse(textBox6.Text)+1;
n2=int.Parse(textBox7.Text);
a=shu.Next(n1,n2);
b=shu.Next(n1,a);
textBox1.Text = a.ToString();
textBox2.Text = b.ToString();
textBox3.Text = "";
textBox4.Text = "";
count++;
}
private void button1_Click_1(object sender, EventArgs e)
{
Form2 da = new Form2();
da.ShowDialog();
}
private void timer1_Tick(object sender, EventArgs e)//计时器
{
if (t <= 0)
{
timer1.Enabled = false;
textBox4.Enabled = false;
MessageBox.Show("时间到!");
Form2 da = new Form2();
da.ShowDialog();
}
t = t - 1;
textBox5.Text = t.ToString();
}
private void textBox4_KeyDown(object sender, KeyEventArgs e)//单击Enter确定计算结果和计算的总题数
{
int s;
s=int.Parse(textBox8.Text);
if (e.KeyCode == Keys.Enter)
{
if (s == count)
{
textBox4.Enabled = false;
Form2 ft = new Form2();
ft.ShowDialog();
}
an();
}
}
}
}
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.right / (double)(Form1.count)) * 100).ToString() + "%";//正确率
}
}
}
四、测试


五、psp耗时
| PSP2.1 | Personal Software Process Stages | Time(h) |
| Planning | 计划 | 10 |
| • Estimate | 估计这个任务需要多长时间 | 8 |
| Development | 开发 | |
| • Analysis | 需求分析 | 2 |
| • Design Spec | 生成设计文档 | |
| • Coding Standard | 代码规范 | 1 |
| • Design | 具体设计 | 1.5 |
| • Coding | 具体代码 | 3 |
| • Code Review | 代码复审 | 1 |
| • Text | 测试 | 0.5 |
| Reporting | 报告 | |
| • Test Report | 测试报告 | |
| • Size Measurement | 计算工作量 |
0.5 |
| • Postmortem | 事后总结 |
1.5 |
六、总结
就这个程序来说,感觉有些难,两个人打代码查资料、扒书打了半天才勉强完成,结果虽然有些差强人意但总归是完成了。

浙公网安备 33010602011771号