您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“如何使用編程語言實現隨機點名程序”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“如何使用編程語言實現隨機點名程序”這篇文章吧。
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; } } }
以上是“如何使用編程語言實現隨機點名程序”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。