2013-02-27 126 views
-1

我想制作一个简单的程序(抽奖号码生成器),该程序在特定范围内接受数字并将其洗牌“n”次,在每次洗牌后它会选择一个随机数并将其从给定范围的列表移动到新列表中,并且将其执行“n”次(直到它选择特定数量的数字,准确地说是7)。我找到了一个完全符合该算法的算法(扩展方法或混排通用列表)。但是我并不是那么喜欢编程,而且我在将结果(带有绘制数字的列表)显示给TextBox或Label时遇到了问题,但是我已经将它与MessageBox配合使用。但有了TextBox/Label,我得到错误“名称*在当前上下文中不存在”。我已经搜索了一个解决方案,但没有任何帮助。C#在当前上下文中不存在名称brojevi

下面的代码:

 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; 
     } 
    } 
} 

}

+0

这可能是一个作用域的问题:http://msdn.microsoft.com/en-us/library/aa691132 %28VS.71%29.aspx。然而,很难说,因为你没有说错误在哪里。 – Bobson 2013-02-27 20:47:14

+1

显示MessageBox的代码在哪里? – XORcist 2013-02-27 20:47:27

+0

从字面上看,“名称*不存在”还是“*”表示特定的符号? – 2013-02-27 20:48:21

回答

1

的问题是,brojevi(无论TextBoxLabel)未在扩展方法的范围来限定,这是一个Control所以应该在Form来限定。所以,当你洗你的号码,把它们放在TextBoxbutton1_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; 
+0

这段代码显示“numbers.Shuffle”的内容(被混洗后的随机顺序为39个数字),但我需要显示“newList”的内容,因为它只包含7个绘制的数字。 TextBox“brojevi”就像你写过的,超出了混洗列表的范围,所以我需要一种方法将TextBox“brojevi”放入类的范围或将值从“newList”导出到文本框“brojevi”。 – 2013-02-28 15:27:26

+0

@JamesDawkins现在检查答案,我认为现在更清楚。 – 2013-02-28 15:50:46

+0

完成了工作。谢谢你的帮助! :-) – 2013-02-28 16:52:25

1

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

相关问题