日期:2014-05-18 浏览次数:20839 次
string[] ss = new string[] { "a", "b", "c", "d" }; Random rand = new Random(); for (int i = 0; i < 3; i++) { MessageBox.Show(ss[rand.Next(ss.Length)]); }
------解决方案--------------------
把名字放到数组中,点击按钮后产生一个0~19之间的随机数,文本框显示对应的名字
public class Form1 : Form { // button1, textbox1, .... string[] m_names = {"名字1", "名字2", ...}; Random rand = new Random(); private void button1_Click(object sender, EventArgs e) { int i = rand.Next(0, m_names.Length); textbox1.Text = m_names[i]; } }
------解决方案--------------------
Random r = new Random(); private void button5_Click(object sender, EventArgs e) { string[] name = new string[] {"张三","李四","王五" }; MessageBox.Show(name[r.Next(name.Length)]); }
------解决方案--------------------