一个测试“专注”的小程序

来源:互联网 发布:我要学编程 编辑:程序博客网 时间:2024/05/16 11:13

这个程序是受到了同学的启发,起初他让我测试一下从一个随机产生的1~25数字中依次找到这25个数,然后看看耗费的时间。呵呵,然后觉得挺有趣,就自己试着写了写这个程序。我自己考虑的比较简单,比如没有考虑点击错误,默认我们点击的顺序是从1开始到25,且没有错误发生。

代码如下:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows;using System.Windows.Controls;using System.Windows.Data;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Imaging;using System.Windows.Navigation;using System.Windows.Shapes;using System.Windows.Threading;namespace TestYourConcentration{    /// <summary>    /// MainWindow.xaml 的交互逻辑    /// </summary>    public partial class MainWindow : Window    {        public MainWindow()        {            InitializeComponent();        }        int[] array = new int[25];        private void Window_Loaded(object sender, RoutedEventArgs e)        {            lbSecond.Foreground = Brushes.Red;//更改label的文本的颜色            lbMinute.Foreground = Brushes.Red;            //string strBtnName = "";            Random r = new Random();                       //string sb = "";//用于判断生成的随机数是否已经存在            //随机产生1~25且不重复的数字            for(int i=0;i<5;++i)                for(int j=0;j<5;j++)                {                    int temp=r.Next(1, 26);                    while(!array.Contains(temp))//如果数组不包含该元素                    {                        array[i * 5 + j] = temp;                    }                    while (array.Contains(temp))//如果已经包含该元素                    {                        temp = r.Next(1, 26);//重新生成随机数                        if (!array.Contains(temp))//如果重新生成的随机数不存在                        {                            array[i * 5 + j] = temp;                            break;                        }                        else //重新生成的随机数已经存在                            continue;                    }                                    }                        //给按钮赋值            btn11.Content = array[0];  string str0 = array[0].ToString(); //"btn11";            btn12.Content = array[1];  string str1 = array[1].ToString(); //"btn12";            btn13.Content = array[2];  string str2 = array[2].ToString(); //"btn13";            btn14.Content = array[3];  string str3 = array[3].ToString(); //"btn14";            btn15.Content = array[4];  string str4 = array[4].ToString(); //"btn15";            btn21.Content = array[5];  string str5 = array[5].ToString(); //"btn21";            btn22.Content = array[6];  string str6 = array[6].ToString();   // "btn22";            btn23.Content = array[7];  string str7 = array[7].ToString();   //"btn23";            btn24.Content = array[8];  string str8 = array[8].ToString();   //"btn24";            btn25.Content = array[9];  string str9 = array[9].ToString();   //"btn25";            btn31.Content = array[10]; string str10 = array[10].ToString();  // "btn31";            btn32.Content = array[11]; string str11 = array[11].ToString(); //"btn32";            btn33.Content = array[12]; string str12 = array[12].ToString(); //"btn33";            btn34.Content = array[13]; string str13 = array[13].ToString(); //"btn34";            btn35.Content = array[14]; string str14 = array[14].ToString(); //"btn35";            btn41.Content = array[15]; string str15 = array[15].ToString(); //"btn41";            btn42.Content = array[16]; string str16 = array[16].ToString(); //"btn42";            btn43.Content = array[17]; string str17 = array[17].ToString(); //"btn43";            btn44.Content = array[18]; string str18 = array[18].ToString(); //"btn44";            btn45.Content = array[19]; string str19 = array[19].ToString(); //"btn45";            btn51.Content = array[20]; string str20 = array[20].ToString(); // "btn51";            btn52.Content = array[21]; string str21 = array[21].ToString(); //"btn52";            btn53.Content = array[22]; string str22 = array[22].ToString(); // "btn53";            btn54.Content = array[23]; string str23 = array[23].ToString(); //"btn54";            btn55.Content = array[24]; string str24 = array[24].ToString(); //"btn55";        }        //为了便于管理计时器,在此设置为全局变量        DispatcherTimer wpfTimer = new System.Windows.Threading.DispatcherTimer();                int index=100;//初始化一个索引的位置,值为除了0与24之间的数,防止意外        int iRestartCount = 0;        private void btnStart_Click(object sender, RoutedEventArgs e)        {            //DispatcherTimer wpfTimer = new System.Windows.Threading.DispatcherTimer();            //wpfTimer.Tick += new EventHandler(dispatcherTimer_Tick);            //wpfTimer.Interval = new TimeSpan(0, 0, 1);                       //int iRestartCount = 0;                        if (btnStart.Content.ToString() == "重新开始")            {                iRestartCount++;//记录点击重新开始的次数                //将开始的计时器关闭                wpfTimer.Stop();                wpfTimer.Tick -= new EventHandler(dispatcherTimer_Tick);                //重新置为0                min = 0;                tick = 0;                if(iRestartCount>=1)                {                    wpfTimer.Tick -= new EventHandler(RestartDispatcherTimer_Tick);                }                             //启用重新开始的计时器                wpfTimer.Tick += new EventHandler(RestartDispatcherTimer_Tick);                wpfTimer.Interval = new TimeSpan(0, 0, 1);                wpfTimer.Start();            }            if(btnStart.Content.ToString()=="开始")            {                wpfTimer.Tick += new EventHandler(dispatcherTimer_Tick);                wpfTimer.Interval = new TimeSpan(0, 0, 1);                wpfTimer.Start();            }                        //只需要找到25,如果25被点击了,则停止计时             index = Array.IndexOf(array, 25);//index记录了25的索引             btnStart.Content = "重新开始";          }        int min = 0;        int tick = 0;        private void dispatcherTimer_Tick(object sender, EventArgs e)        {            tick++;            lbSecond.Content = tick;                        if((tick!=0)&&(Convert.ToInt32(tick)%60==0))            {                min++;                lbMinute.Content = min;                tick = 0;            }        }        private void RestartDispatcherTimer_Tick(object sender, EventArgs e)        {            tick++;            lbSecond.Content = tick;            if ((tick != 0) && (Convert.ToInt32(tick) % 60 == 0))            {                min++;                lbMinute.Content = min;                tick = 0;            }        }        //用于判断按钮是否被点击了        bool b11 = false, b12 = false, b13 = false, b14 = false, b15 = false;        bool b21 = false, b22 = false, b23 = false, b24 = false, b25 = false;        bool b31 = false, b32 = false, b33 = false, b34 = false, b35 = false;        bool b41 = false, b42 = false, b43 = false, b44 = false, b45 = false;        bool b51 = false, b52 = false, b53 = false, b54 = false, b55 = false;/********************循环判断25是否被点击***********************/        private void btn11_Click(object sender, RoutedEventArgs e)        {            b11 = true;            if (index == 0)            {                wpfTimer.Stop();                MessageBox.Show("耗时:" + lbMinute.Content + " 分" + lbSecond.Content + " 秒");             }        }        private void btn12_Click(object sender, RoutedEventArgs e)        {            b12 = true;            if (index == 1)            {                wpfTimer.Stop();                MessageBox.Show("耗时:" + lbMinute.Content + " 分" + lbSecond.Content + " 秒");            }        }        private void btn13_Click(object sender, RoutedEventArgs e)        {            b13 = true;            if (index == 2)            {                wpfTimer.Stop();                MessageBox.Show("耗时:" + lbMinute.Content + " 分" + lbSecond.Content + " 秒");            }        }        private void btn14_Click(object sender, RoutedEventArgs e)        {            b14 = true;            if (index == 3)            {                wpfTimer.Stop();                MessageBox.Show("耗时:" + lbMinute.Content + " 分" + lbSecond.Content + " 秒");            }        }        private void btn15_Click(object sender, RoutedEventArgs e)        {            b15 = true;            if (index == 4)            {                wpfTimer.Stop();                MessageBox.Show("耗时:" + lbMinute.Content + " 分" + lbSecond.Content + " 秒");            }        }        private void btn21_Click(object sender, RoutedEventArgs e)        {            b21 = true;            if (index == 5)            {                wpfTimer.Stop();                MessageBox.Show("耗时:" + lbMinute.Content + " 分" + lbSecond.Content + " 秒");            }        }        private void btn22_Click(object sender, RoutedEventArgs e)        {                        b22 = true;            if (index ==6)            {                wpfTimer.Stop();                MessageBox.Show("耗时:" + lbMinute.Content + " 分" + lbSecond.Content + " 秒");            }                    }        private void btn23_Click(object sender, RoutedEventArgs e)        {            b23 = true;            if (index == 7)            {                wpfTimer.Stop();                MessageBox.Show("耗时:" + lbMinute.Content + " 分" + lbSecond.Content + " 秒");            }        }        private void btn24_Click(object sender, RoutedEventArgs e)        {            b24 = true;            if (index == 8)            {                wpfTimer.Stop();                MessageBox.Show("耗时:" + lbMinute.Content + " 分" + lbSecond.Content + " 秒");            }        }        private void btn25_Click(object sender, RoutedEventArgs e)        {            b25 = true;            if (index == 9)            {                wpfTimer.Stop();                MessageBox.Show("耗时:" + lbMinute.Content + " 分" + lbSecond.Content + " 秒");            }        }        private void btn31_Click(object sender, RoutedEventArgs e)        {            b31 = true;            if (index == 10)            {                wpfTimer.Stop();                MessageBox.Show("耗时:" + lbMinute.Content + " 分" + lbSecond.Content + " 秒");            }        }        private void btn32_Click(object sender, RoutedEventArgs e)        {            b32 = true;            if (index == 11)            {                wpfTimer.Stop();                MessageBox.Show("耗时:" + lbMinute.Content + " 分" + lbSecond.Content + " 秒");            }        }        private void btn33_Click(object sender, RoutedEventArgs e)        {            b33 = true;            if (index == 12)            {                wpfTimer.Stop();                MessageBox.Show("耗时:" + lbMinute.Content + " 分" + lbSecond.Content + " 秒");            }        }        private void btn34_Click(object sender, RoutedEventArgs e)        {            b34 = true;            if (index == 13)            {                wpfTimer.Stop();                MessageBox.Show("耗时:" + lbMinute.Content + " 分" + lbSecond.Content + " 秒");            }        }        private void btn35_Click(object sender, RoutedEventArgs e)        {            b35 = true;            if (index == 14)            {                wpfTimer.Stop();                MessageBox.Show("耗时:" + lbMinute.Content + " 分" + lbSecond.Content + " 秒");            }        }        private void btn41_Click(object sender, RoutedEventArgs e)        {            b41 = true;            if (index == 15)            {                wpfTimer.Stop();                MessageBox.Show("耗时:" + lbMinute.Content + " 分" + lbSecond.Content + " 秒");            }        }        private void btn42_Click(object sender, RoutedEventArgs e)        {            b42 = true;            if (index == 16)            {                wpfTimer.Stop();                MessageBox.Show("耗时:" + lbMinute.Content + " 分" + lbSecond.Content + " 秒");            }        }        private void btn43_Click(object sender, RoutedEventArgs e)        {            b43 = true;            if (index == 17)            {                wpfTimer.Stop();                MessageBox.Show("耗时:" + lbMinute.Content + " 分" + lbSecond.Content + " 秒");            }        }        private void btn44_Click(object sender, RoutedEventArgs e)        {            b44 = true;            if (index ==18)            {                wpfTimer.Stop();                MessageBox.Show("耗时:" + lbMinute.Content + " 分" + lbSecond.Content + " 秒");            }        }        private void btn45_Click(object sender, RoutedEventArgs e)        {            b45 = true;            if (index == 19)            {                wpfTimer.Stop();                MessageBox.Show("耗时:" + lbMinute.Content + " 分" + lbSecond.Content + " 秒");            }        }        private void btn51_Click(object sender, RoutedEventArgs e)        {            b51 = true;            if (index == 20)            {                wpfTimer.Stop();                MessageBox.Show("耗时:" + lbMinute.Content + " 分" + lbSecond.Content + " 秒");            }        }        private void btn52_Click(object sender, RoutedEventArgs e)        {            b52 = true;            if (index == 21)            {                wpfTimer.Stop();                MessageBox.Show("耗时:" + lbMinute.Content + " 分" + lbSecond.Content + " 秒");            }        }        private void btn53_Click(object sender, RoutedEventArgs e)        {            b53 = true;            if (index == 22)            {                wpfTimer.Stop();                MessageBox.Show("耗时:" + lbMinute.Content + " 分" + lbSecond.Content + " 秒");            }        }        private void btn54_Click(object sender, RoutedEventArgs e)        {            b54= true;            if (index == 23)            {                wpfTimer.Stop();                MessageBox.Show("耗时:" + lbMinute.Content + " 分" + lbSecond.Content + " 秒");            }        }        private void btn55_Click(object sender, RoutedEventArgs e)        {            b55 = true;            if (index == 24)            {                wpfTimer.Stop();                MessageBox.Show("耗时:" + lbMinute.Content + " 分" + lbSecond.Content + " 秒");            }        }        private void Instruments_Click(object sender, RoutedEventArgs e)        {            ScoreInstructionWindow siw = new ScoreInstructionWindow();            siw.Show();        }    }}
初始化界面:

点击开始后,按钮变成重新开始,并开始计时:


当点击到25这个按钮的时候,停止计时,并显示所耗费的时间:


然后本次测试结束,还可以重新进行测试(有一点没做:点击重新开始时,没有重新生成25个随机数)



0 0
原创粉丝点击