【发布时间】:2014-06-27 17:04:29
【问题描述】:
我尝试编写一个模拟彩票的简单程序,但我遇到了一些我无法理解的行为,也无法修复:
为了简单起见,我排除了与问题无关的代码
程序:点击它应该获取用户输入的六个不同的数字(1 到 49 之间),获取六个不同的随机数(1 到 49 之间)比较它们并重复获取随机数并与输入的比较,直到有三个匹配.
-
相关的是,我在按钮单击时调用函数 GetResults() 并将两个参数传递给它(下面的方法定义)。我将其简化为单击按钮向您展示。那里有一些条件和函数调用,但它们正在工作,即使没有它们也存在问题,因此底部的图像示例可能会有所不同。
private void btnCheck_Click(object sender, EventArgs e) { lotto.GetResults(3, ref listRndNumLabels); lblMatches.Text = lotto.CurrentMatches.ToString(); lblTryCounter.Text = lotto.NumberOfTries.ToString(); lblBalance.Text = lotto.Balance.ToString() + " zł"; lblThreesAmmount.Text = lotto.ThreesAmmount.ToString(); lblFoursAmmount.Text = lotto.FoursAmmount.ToString(); lblFivesAmmount.Text = lotto.FivesAmmount.ToString(); lblSixesAmmount.Text = lotto.SixesAmmount.ToString(); } -
GetResults() 方法采用 3 作为所需匹配的数量和最后更新的标签列表
public void GetResults(int Choice, ref List<Label> listLblRndNum) { _currentMatches = 0; int desiredNumberOfMatches = Choice; // while we got other ammount of matches than three, go again while (_currentMatches != desiredNumberOfMatches) { _numberOfTries++; // get list of mutually exclusive 6 numbers betweeen 1 and 49 var tempList = GetRndNums(); // insert random numbers to list _listLotteryNumbers.Clear(); for (int i = 0; i < 6; i++) { _listLotteryNumbers.Insert(i, tempList[i]); } _balance -= _ticketCost; _currentMatches = 0; // get number of matches for (int i = 0; i < 6; i++) { foreach (int num in _listLotteryNumbers) { if (_listSelectedNumbers[i] == num) { _currentMatches++; } } } //FrmLotto.lbResults.Items.Add(_numberOfTries.ToString() + " - _currentMatches: " + _currentMatches.ToString()); //FrmLotto.lbResults.Items.Add(_numberOfTries.ToString() + " - tempList { " + tempList[0] + " " + tempList[1] + " " + tempList[2] + " " + tempList[3] + " " + tempList[4] + " " + tempList[5] + " }"); //FrmLotto.lbResults.Items.Add(_numberOfTries.ToString() + " - _listLotteryNumbers { " + _listLotteryNumbers[0] + " " + _listLotteryNumbers[1] + " " + _listLotteryNumbers[2] + " " + _listLotteryNumbers[3] + " " + _listLotteryNumbers[4] + " " + _listLotteryNumbers[5] + " }"); //FrmLotto.lbResults.Items.Add(_numberOfTries.ToString() + " - _listSelectedNumbers { " + _listSelectedNumbers[0] + " " + _listSelectedNumbers[1] + " " + _listSelectedNumbers[2] + " " + _listSelectedNumbers[3] + " " + _listSelectedNumbers[4] + " " + _listSelectedNumbers[5] + " }"); // update stats if (_currentMatches == 3) { _threesAmmount++; _balance += 10; } else if (_currentMatches == 4) { _foursAmmount++; _balance += 100; } else if (_currentMatches == 5) { _fivesAmmount++; _balance += 3500; } else if (_currentMatches == 6) { _sixesAmmount++; _balance += 1000000; } //FrmLotto.lbResults.Items.Add(_numberOfTries.ToString() + " - Threes Ammount right after updating: " + _threesAmmount); //FrmLotto.lbResults.Items.Add(""); // this gets out of the loop if user has chosen from ddl to run once, it is irrelevant here if (desiredNumberOfMatches == -1) break; } // finally update Labels with the desired result for (int i = 0; i < 6; i++) { listLblRndNum[i].Text = _listLotteryNumbers[i].ToString(); } } -
这是获取随机数的函数:
public List<int> GetRndNums() { List<int> listRndNums = new List<int>(); Random rndNum = new Random(); for (int i = 0; i < 6; i++) { int myNum = 0; do myNum = rndNum.Next(1, 49); while (listRndNums.Contains(myNum)); listRndNums.Add(myNum); } listRndNums.Sort(); return listRndNums; }
如果循环运行一次,或者每次循环后有延迟,或者我在循环中设置断点,程序就会按预期运行。
否则会出现意外行为,循环运行多次相同的数据(相同的列表),我不明白为什么。
看图片:
- 程序运行一次,我点击了五次按钮,显示效果很好:
(btw = Sprawdź = 检查,raz = 一次,做 pierwszej trójki = 直到 3 场比赛)
- 当我选择直到 3 个匹配项(或单击上面代码示例中的按钮)时,我收到错误的结果,循环针对相同的值运行多次。
非常感谢您的帮助,我正在学习,我知道代码的许多部分可以改进,许多部分仅用于临时调试目的。但是这种行为,我根本不明白。
【问题讨论】:
-
仅供参考 - 将列表作为 ref 参数传递是多余的,列表是默认通过引用传递的实例