WindowsForm 大樂透 使用C#

-----練習,使用for迴圈-----


         private void btnRun_Click(object sender, EventArgs e)
        {
            int iMin = Convert .ToInt16(txtMin.Text) -1;
            int iMax = Convert .ToInt16(txtMax.Text);
            Random rdResult = new Random();


            int[] iResult = new int[6];
            //array 歸零
            for (int i = 0; i <= 5; i++)
                iResult[i] = 0;
            //存取亂數的變數
            int iNumber;
            for (int i = 0; i <= 5; i++)
            {
                //產生亂數
                iNumber = rdResult.Next(iMin -1, iMax) + 1;
                for (int j = 0; j <= 5;)
                {
                    //若亂數重複
                    if (iNumber == iResult[j])
                    {
                        iNumber = rdResult.Next(iMin -1, iMax) + 1;
                        j = 0;
                    }
                    else
                        j++;
                }
                //沒有重複就塞到array
                iResult[i] = iNumber;
            }
            for (int i = 0; i <= 5; i++)
            {
                //類似Web form 的 FindControl
                (( TextBox)pn1.Controls.Find("txtResult" + i.ToString(), true)[0]).Text
                    = iResult[i].ToString();
            }
        }

沒有留言: