如何使用编程语言实现随机点名程序

这篇文章主要为大家展示了“如何使用编程语言实现随机点名程序”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“如何使用编程语言实现随机点名程序”这篇文章吧。

如何使用编程语言实现随机点名程序

using System;  using System.Collections.Generic;  using System.IO;  using System.Windows.Forms;  using System.Drawing;   namespace Randoms  {      public partial class Main : Form      {          FileStream fs = null;          StreamReader sr = null;          //StreamWriter sw = null;          List<string> names = new List<string>();          Random rd = new Random();          int jg = 0;          //string syspath = System.Environment.CommandLine;//当前程序路径          string syspath = System.Windows.Forms.Application.StartupPath;           public Main()          {              InitializeComponent();               //GetData();//获取数据                                          }           private void GetData()          {              string Namesaddress = syspath + "\\Names.txt";              try             {                   fs = new FileStream(Namesaddress, FileMode.Open);                   sr = new StreamReader(fs);                   //string count=sr.ReadLine();                   //while (count!=null)                   //{                  //    name.Add(count.ToString());                  //}                    while (true)                  {                      string s = sr.ReadLine();                      if (s != null)                          names.Add(s);                      else                         break;                  }               }              catch (Exception)              {                   throw;              }              finally             {                  sr.Close();                  fs.Close();              }          }           /// <summary>          /// 开始点名          /// </summary>          /// <param name="sender"></param>          /// <param name="e"></param>          private void button1_Click(object sender, EventArgs e)          {              this.button2.Enabled = true;              lab.ForeColor = Color.Black;              if (names.Count == 0)              {                   this.time.Enabled = false;                  lab.Text = "Please Reload At First";                }              else             {                  this.time.Enabled = true;              }           }          /// <summary>          /// 计数器          /// </summary>          /// <param name="sender"></param>          /// <param name="e"></param>          private void time_Tick(object sender, EventArgs e)          {                                int rf = names.Count;                  jg = rd.Next(0, rf);                  lab.Text = names[jg];                                                   }          /// <summary>          /// 停止          /// </summary>          /// <param name="sender"></param>          /// <param name="e"></param>          private void button2_Click(object sender, EventArgs e)          {               if (names.Count == 0)              {                  this.time.Enabled = false;                  lab.Text = "The End";                  //GetData();              }              else             {                  lab.ForeColor = Color.Blue;                  this.time.Enabled = false;                  names.RemoveAt(jg);              }                                          }          /// <summary>          /// 加载所有人          /// </summary>          /// <param name="sender"></param>          /// <param name="e"></param>          private void Main_Load(object sender, EventArgs e)          {              string Namesaddress = syspath + "\\Names.txt";              if (System.IO.File.Exists(Namesaddress) )              {                  GetData();              }              else             {                   MessageBox.Show("查看Names.txt文件是否存在");                  Application.Exit();               }          }           /// <summary>          /// 从新加载          /// </summary>          /// <param name="sender"></param>          /// <param name="e"></param>          private void button3_Click(object sender, EventArgs e)          {              lab.ForeColor = Color.Black;              this.time.Enabled = false;              lab.Text = "Load Complete";              GetData();              this.button2.Enabled = false;          }      }  }

以上是“如何使用编程语言实现随机点名程序”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!