(C#)Random实现随机点名

发布时间 2023-04-07 08:54:07作者: 哑ba湖大水怪

namespace WindowsFormsApp3

{
  public partial class Form1 : Form
  {

    //实例化字符串,设置字符串长度与内容
    string[] student=new string[7] { "张三","李四","王五","赵六","hello","world","hello world"};
    public Form1()
    {
      InitializeComponent();
    }

    //在WinForm界面布置Label作为名字显示,布置Button触发随机点名事件

    private void button1_Click(object sender, EventArgs e)
    {
      this.label1.Text = "";
      Random name= new Random();
      int num=name.Next(7);
      this.label1.Text = student[num];
    }
  }
}