樂透彩寫法(陽春到不行版)
最近有幾個朋友去面試,公司都要求寫樂透,現在是怎樣><,雖然網路上一堆,不過我試著自己寫一個,大家參考看看
public void RandomNum()
{
Random rand;
List list = new List();
for (int i = 1; i <= 7; i++)
{
string strBuf = "";
//unchecked忽略overflow並給值-2
//DateTime.Now.Ticks 奈秒
rand = new Random(i * i * unchecked((int)DateTime.Now.Ticks));
//rand = new Random(i * ((int)DateTime.Now.Ticks));
strBuf = rand.Next(47).ToString();//0~46 共47個
if (strBuf != "0" && i != 0 && list.IndexOf(strBuf) == -1)//如果不是零且i不是第一個且沒有找到
list.Add(strBuf);
else
i--;
}
for (int k = 0; k <= 6; k++)
{
foreach (Control c in this.Controls)
{
if (c.Name == string.Format("textBox{0}", k + 1))
{
c.Text = list[k].ToString();
}
}
}
}
留言
張貼留言
您好,我是 Lawrence,這裡是我的開發筆記的網誌,如果你對我的文章有任何疑問或者有錯誤的話,歡迎留言讓我知道。