标签:
我用的是窗体来做的 老师教过 比较简单的方法。
一共用了三个窗体
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 Windows窗体的调用
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public static int Count = 0;
private int t = 60;
public static int right = 0;
private void button1_Click(object sender, EventArgs e)
{
label2.Text = t.ToString();
timer1.Enabled = true;
timer1.Interval = 1000;
timer1.Start();
RandomNum();
}
private void RandomNum()
{
Random ran=new Random();
int n1,n2;
n1=ran.Next(1,101);
n2=ran.Next(1,101);
textBox1.Text=n1.ToString();
textBox2.Text=n2.ToString();
textBox3.Text="";
Count++;
}
private void timer1_Tick(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;
label2.Text = t.ToString();
}
private void button2_Click(object sender, EventArgs e)
{
textBox3.Enabled = false;
Form2 frm2 = new Form2();
frm2.ShowDialog();
}
private void button3_Click(object sender, EventArgs e)
{
new Form3().Show();
}
private void textBox3_KeyDown(object sender, KeyEventArgs e)
{
int sum;
sum = int.Parse(textBox1.Text) + int.Parse(textBox2.Text);
if (e.KeyCode == Keys.Enter)
{
if (textBox3.Text == sum.ToString())
right++;
RandomNum();
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
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 Windows窗体的调用
{
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() + "%";
}
}
}
Form3:
代码编辑如下:
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 Windows窗体的调用
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
private void Form3_Load(object sender, EventArgs e)
{
}
}
}
主代码为:
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 Windows窗体的调用
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
private void Form3_Load(object sender, EventArgs e)
{
}
}
}
测试图为:
思路就是用窗体构建出简单模型,然后编写代码,用时1小时。
标签:
原文地址:http://www.cnblogs.com/houqiqi/p/4852511.html