闲来无事帮银行朋友写了一个练习输数字准确率的程序
代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
using System.Windows.Forms;
namespace InputNumDemo
{
public partial class Form1 : Form
{
System.Timers.Timer timer;
int countNum = 0;
int rightNum = 0;
int wrongNum = 0;
int listIndex = 0;
int mint = 4;
int scss = 59;
List<double> scoreList = new List<double>();//创建了一个空列表;
public Form1()
{
InitializeComponent();
CheckForIllegalCrossThreadCalls = false;
timeLeftMin.Text = "5分";
timeLeftSec.Text = "0秒";
}
public void test()
{
//5min内要做的事情
//1.随机生成1k个数据存好
setRandomNum(); //scoreList中存入5条数据
showAndCompNum();//初始时固定展示前三个,后续使用代码
}
public void showAndCompNum()
{
//第一次点击开始,只显示三个
now.Text = scoreList[0].ToString();
next.Text = scoreList[1].ToString();
nextnext.Text = scoreList[2].ToString();
}
public void setRandomNum()
{
Random ran = new Random();
for (int i = 0; i < 1000; i++)
{
scoreList.Add(NextDouble(ran, 10, 1000, 2));
countNum++;
}
}
public double NextDouble(Random ran, double minValue, double maxValue, int decimalPlace)
{
double randNum = ran.NextDouble() * (maxValue - minValue) + minValue;
return Convert.ToDouble(randNum.ToString("f" + decimalPlace));
}
public void start_Click(object sender, EventArgs e)
{
//初始化一些参数
timeLeftMin.Text = "4分";
total.Text = "0";
right.Text = "0";
wrong.Text = "0";
test();
start.Enabled = false;
timer = new System.Timers.Timer();
timer.Enabled = true;
timer.Start();
this.timer1.Start();
timer.AutoReset = false;
timer.Interval = 300000; //执行间隔时间,单位为毫秒; 这里实际间隔300000为5分钟
timer.Elapsed += new System.Timers.ElapsedEventHandler(end_Click); //5min后执行的程序
//this.timer1.Tick += new System.EventHandler(this.timer1_Tick); //设置每间隔3000毫秒(3秒)执行一次函数timer1_Tick
}
//时间到了or用户按下end按钮都会执行
private void end_Click(object sender, EventArgs e)
{
timer.Dispose();
timer1.Dispose();
MessageBox.Show("5min计时结束");
start.Enabled = true;
timeLeftMin.Text = "5分";
timeLeftSec.Text = "0秒";
}
private void input_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Enter)
{
//2.获取右边的输入,和左边第一个对比,判断对错后展示
if (input.Text == now.Text)
{
rightNum++;
}
else
{
wrongNum++;
}
//每输入一次,展示成果
total.Text = (rightNum + wrongNum).ToString();
right.Text = rightNum.ToString();
wrong.Text = wrongNum.ToString();
//清空输入框
input.Text = "";
this.input.Focus();
//更新左边数据。l1.Text = "第" + countNum + "个:";
now.Text = scoreList[listIndex + 1].ToString();
Console.WriteLine("enter一次后now的数据为:" + scoreList[listIndex+1].ToString());
next.Text = scoreList[listIndex + 2].ToString();
Console.WriteLine("enter一次后next的数据为:" + scoreList[listIndex + 2].ToString());
nextnext.Text = scoreList[listIndex + 3].ToString();
Console.WriteLine("enter一次后nextnext的数据为:" + scoreList[listIndex + 3].ToString());
listIndex++;
}
}
private void Form1_Activated(object sender, EventArgs e)
{
input.Focus();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (mint >= 0)
{
scss--;
if (scss == 0)
{
mint--;
timeLeftMin.Text = mint.ToString() + "分";
scss = 59;
}
timeLeftSec.Text = scss.ToString() + "秒";
}
}
}
}
界面长这样:
描述:
- 点击开始,左侧便会更新数据,显示即将需要输入的3个数据,并在右上角开始计时。
- 用户在右侧的输入框输入第1个数字;按回车后,右侧输入框清空,左侧原来第2个数据变为第1个,以此类推。
- 用户继续在右侧输入左侧不断变化的第一个数据,程序判定输入的和左侧的书是否一致,并对正确和错误的个数计数。
- 等到5min结束后,跳出提示框时间截止,并结束程序。
PS:此程序还有很多不足,需后续修改(等有时间再说)