新建一个 windows窗体应用程序 项目,在窗体上加一个TextBox控件(textBox1)和一个Button控件(button1),双击Button控件,在button1的点击事件的方法加入代码,如下,启动程序,点击按钮就可以看到随机生成的双色球号码。
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = "";
//生成红色球号码
List<int> _list;
_list = new List<int>(33);
for (int i = 1; i <34; i++)
{
_list.Add(i);
}
Random rd = new Random();
int index;
for (int i = 1; i < 7; i++)
{
index = rd.Next(_list.Count);
textBox1.Text = textBox1.Text + _list[index].ToString();
if (i < 6)
textBox1.Text = textBox1.Text + ", ";
_list.RemoveAt(index);
}
//生成蓝色球号码
Random rb = new Random();
textBox1.Text = textBox1.Text + " + " + (rb.Next(16)+1).ToString();
}