本人新手一枚,想用C#窗体写一个双色球的游戏,请高手们指点指点~~
南京城某君 2014-05-08 02:23:11 //用于存放1到33这33个
List<int> container = new List<int>(33);
List<int> result = new List<int>(6);
//定义抽奖次数,双色球6+1
int j = 1;
int count = 0;
public void Award()
{
count++;
if (count == 7)
{
sorkAward();
MessageBox.Show("本期开奖结束!");
this.btnAward.Enabled = false;
}
}
//初始化数组
public void InitArray()
{
for (int i = 1; i < 33; i++)
{
container.Add(i);
}
}
public void RedRandom()
{
Random rd = new Random();
//随机生成蓝球
Random rdblue = new Random();
int mathblue = rdblue.Next(1, 12);
int index = 0;
int value = 0;
//从[0,container.Count]中取一个随机值,保证这个值不会超过container的元素个数
index = rd.Next(0, container.Count);
value = container[index];
container.RemoveAt(index);
//遍历所有文本框
foreach (Control c in Controls)
{
//判断类型为TextBox
if (c is TextBox)
{
if (c.Name == "txt1" && txt1.Text == "")
{
this.txt1.Text = value.ToString();
this.txt2.Text = "";
this.txt3.Text = "";
this.txt4.Text = "";
this.txt5.Text = "";
this.txt6.Text = "";
this.txtBlue.Text = "";
}
else if (c.Name == "txt2" && txt2.Text == "")
{
this.txt2.Text = value.ToString();
this.txt3.Text = "";
this.txt4.Text = "";
this.txt5.Text = "";
this.txt6.Text = "";
this.txtBlue.Text = "";
}
else if (c.Name == "txt3" && txt3.Text == "")
{
this.txt3.Text = value.ToString();
this.txt4.Text = "";
this.txt5.Text = "";
this.txt6.Text = "";
this.txtBlue.Text = "";
}
else if (c.Name == "txt4" && txt4.Text == "")
{
this.txt4.Text = value.ToString();
this.txt5.Text = "";
this.txt6.Text = "";
this.txtBlue.Text = "";
}
else if (c.Name == "txt5" && txt5.Text == "")
{
this.txt5.Text = value.ToString();
this.txt6.Text = "";
this.txtBlue.Text = "";
}
else if (c.Name == "txt6" && txt6.Text == "")
{
this.txt6.Text = value.ToString();
this.txtBlue.Text = "";
}
else if (c.Name == "txtBlue" && txtBlue.Text == "")
{
this.txtBlue.Text = mathblue.ToString();
}
}
}
result.Add(value);
container.RemoveAt(index);
}
//点击出奖
private void button1_Click(object sender, EventArgs e)
{
j++;
RedRandom();
Award();
}
//下期开奖
private void button2_Click(object sender, EventArgs e)
{
//初始化数组
InitArray();
count = 0;
foreach (Control c in Controls)
{
if (c is TextBox)
{
c.Text = "";
}
}
this.btnAward.Enabled = true;
}
//开奖后排序,如何依次对文本框赋值?
public void sorkAward() {
result.Sort();
}
}
---------------------------------------------------------
问题一:如何排序生成的中奖号码并显示在文本框内
问题二:如何生成不重复的随机数