-1

我想制作一个简单的程序(彩票号码生成器),它采用特定范围内的数字并将它们随机播放“n”次,在每次随机播放后它选择一个随机数并将其从给定范围的列表中移动到一个新的列表,并执行“n”次(直到它选择特定数量的数字,确切地说是 7)。我找到了一种完全可以做到这一点的算法(扩展方法或改组通用列表)。但我不是那么喜欢编程,我在将结果(带有绘制数字的列表)显示到 TextBox 或 Label 时遇到问题,但是我已经让它与 MessageBox 一起工作。但是使用 TextBox/Label 我收到错误“名称*在当前上下文中不存在”。我已经用谷歌搜索了一个解决方案,但没有任何帮助。

这是代码:

      private void button1_Click(object sender, EventArgs e)
      {
         List<int> numbers;
         numbers = Enumerable.Range(1, 39).ToList();
         numbers.Shuffle();
      }

      private void brojevi_TextChanged(object sender, EventArgs e)
      {          
      }
  }
}

/// <summary>
/// Class for shuffling lists
/// </summary>
/// <typeparam name="T">The type of list to shuffle</typeparam>
public static class ListShufflerExtensionMethods
{
    //for getting random values
    private static Random _rnd = new Random();

    /// <summary>
    /// Shuffles the contents of a list
    /// </summary>
    /// <typeparam name="T">The type of the list to sort</typeparam>
    /// <param name="listToShuffle">The list to shuffle</param>
    /// <param name="numberOfTimesToShuffle">How many times to shuffle the list,    by default this is 5 times</param>
    public static void Shuffle<T>(this List<T> listToShuffle, int numberOfTimesToShuffle = 7)
    {          
        //make a new list of the wanted type
        List<T> newList = new List<T>();

        //for each time we want to shuffle
        for (int i = 0; i < numberOfTimesToShuffle; i++)
        {
            //while there are still items in our list
            while (listToShuffle.Count >= 33)
            {
                //get a random number within the list
                int index = _rnd.Next(listToShuffle.Count);

                //add the item at that position to the new list
                newList.Add(listToShuffle[index]);

                //and remove it from the old list
                listToShuffle.RemoveAt(index);
            }

            //then copy all the items back in the old list again
            listToShuffle.AddRange(newList);

            //display contents of a list
            string line = string.Join(",", newList.ToArray());
            brojevi.Text = line;

            //and clear the new list
            //to make ready for next shuffling
            newList.Clear();
            break;
        }
    }
}

}

4

2 回答 2

1

ListShufflerExtensionMethods 不知道您的文本框(brojevi),因为它超出了范围。您可以重组并让 Shuffle 返回一个值,然后在调用者范围内设置文本框文本的值。

于 2013-02-27T20:51:29.053 回答
1

问题是brojevi(either TextBoxor Label) 没有在扩展方法的范围内定义,它是 aControl所以它应该在你的Form. 所以,当你打乱你的号码时,把它们放在事件处理程序TextBox的执行过程中button1_Click

删除行:

    string line = string.Join(",", newList.ToArray());
    brojevi.Text = line;

编辑:

您可以像这样更改扩展方法以返回绘制项目的字符串或绘制的项目列表。让我们来看看列表,因为您可能想将这些数字用于其他事情。另外,我看不出洗牌 7 次的意义,因为你只能看到最后一次洗牌。因此,我认为一个就足够了。检查代码:

public static List<T> Shuffle<T>(this List<T> listToShuffle)
        {
            //make a new list of the wanted type
            List<T> newList = new List<T>();


            //while there are still items in our list
            while (listToShuffle.Count >= 33)
            {
                //get a random number within the list
                int index = _rnd.Next(listToShuffle.Count);

                //add the item at that position to the new list
                newList.Add(listToShuffle[index]);

                //and remove it from the old list
                listToShuffle.RemoveAt(index);
            }

            //then copy all the items back in the old list again
            listToShuffle.AddRange(newList);

            return newList;
        }

在 button1_Click1 事件处理程序中,我们可以拥有:

List<int> numbers;
numbers = Enumerable.Range(1, 39).ToList();
List<int> drawnNumbers = numbers.Shuffle();
string line = string.Join(",", drawnNumbers.ToArray());
brojevi.Text = line;
于 2013-02-27T20:55:34.023 回答