2013-03-29 80 views
0

我想創建一個程序,當我單擊按鈕時,在消息框中使用正確的單詞顯示一個隨機類別(來自我創建的類別列表)。在列表C中隨機化字符串時出錯#

當我運行它時,這些類別是隨機的,但是應該與類別對應的正確單詞沒有正確放置。 Image of Error

而且,我知道,一旦程序到達類別的負指數或一旦所有類別的程序會崩潰顯示 Image of Error

,但我想不出用什麼邏輯所以它會自動停止刪除索引,一旦它達到了負值。

代碼:

namespace randomCategory 
{ 
public partial class Form1 : Form 
{ 

    Random rand = new Random(); 
    List<string> categories = new List<string> { "Book Titles", "Movie Titles", "Car Parts", "Human Body Parts", "Transportations" }; 


    public Form1() 
    { 
     InitializeComponent(); 
     listBox1.DataSource = categories; 
    } 

    public void selection() 
    { 
     // logic for setting a random category 
     int index = rand.Next(categories.Count); 
     var category = categories[index]; 


     // logic for assigning the word for a category 
     switch (index) 
     { 
      case 0: 
       MessageBox.Show(category, "Harry Potter"); 
       break; 
      case 1: 
       MessageBox.Show(category, "Summer Wars"); 
       break; 
      case 2: 
       MessageBox.Show(category, "Bumper"); 
       break; 
      case 3: 
       MessageBox.Show(category, "Eyes"); 
       break; 
      case 4: 
       MessageBox.Show(category, "Boat"); 
       break; 
      default: 
       MessageBox.Show("Empty!", "!!!"); 
       break; 
     } 

     categories.RemoveAt(index); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     selection(); 
    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 

    } 
} 
} 
+0

你有沒有想過改變'列表'爲使用'HashSet的,LinkedList的或Dictionary' – MethodMan

+0

@DJKRAZE我有點新的C#,現在試圖尋找如何使用字典,因爲它也推薦其他人說的方式。我想着如何將代碼轉換成HashSet和LinkedList的方法。 – Coolai

+0

我發佈了一個簡單的例子,關於如何將現有的列表轉換爲字典 MethodMan

回答

3

必須設置列表框中選擇以反映您所選擇的隨機指標。

另外,如果列表中沒有項目,則不應該這樣做。所以這個代碼添加到你的方法:

if (categories.Count == 0) 
    return; 

的問題可能是,當你從categories列表中刪除項目,索引號不再與您的switch聲明匹配。例如,您categories開始爲:

{ "Book Titles", "Movie Titles", "Car Parts", "Human Body Parts", "Transportations" }; 

如果從列表中選擇任何項目,它會在你的交換機相匹配。例如,如果您隨機選擇1,則程序將顯示「夏季戰爭」:電影標題。

但是,然後你從列表中刪除該項目。您的列表現在看起來是這樣的:

{ "Book Titles", "Car Parts", "Human Body Parts", "Transportations" }; 

所以你隨機選擇2,這是「人體器官」,因爲你從列表中刪除的項目。

解決此問題的一種方法是創建另一個名爲unusedCategories的列表。初始化這樣的:

List<int> unusedCategories = new List<int> { 0, 1, 2, 3, 4 }; 

現在,您可以選擇從列表中的項目:

int selectedIndex = rand.Next(unusedCategories.Count); 
int index = unusedCategories[selectedIndex]; 
// at this point, index is the index to one of the items in your `categories` list 

switch (index) 
{ 
    .... 
} 

unusedCategories.RemoveAt(selectedIndex); 

,當然,你會改變if聲明:

if (unusedCategories.Count == 0) 
    return; 
+0

實際上,在我的程序中,listbox1的唯一目的僅僅是向用戶展示哪些類別將被隨機顯示,僅此而已。我真正想要發生的事情是讓隨機類別能夠正確鏈接到該類別的正確單詞。 如果控制語句像魅力一樣工作,謝謝! – Coolai

+0

謝謝Jim Mischel先生!您向我提供了關於錯誤出現的原因的代碼和重要細節。 所以,我真的刪除類別列表上的索引是錯誤的,因爲它會導致索引的刪除,導致範圍/計數移動,這將使我的switch語句將索引轉換爲索引的錯誤分配類別。 – Coolai

+0

如Jim所說的解決方案是創建另一個列表,它將替代列表中的索引,但將刪除selectedIndex並仍然保留索引,以便隨機化併合並製作切換條件以使該單詞與正確的類別匹配。謝謝米謝爾先生! :] – Coolai

0

如果您想將現有的string列表轉換爲Dictionary<T,T> 一行代碼可以爲您做

var categories = new List<string> 
{ 
    "Book Titles", 
    "Movie Titles", 
    "Car Parts", 
    "Human Body Parts", 
    "Transportations" 
}; 
var catDict = categories.ToDictionary(c => c); 
+0

我認爲我解決方案的方式並不可取,如果我將它用於其他任務和關聯,則會受到限制。我將更多地考慮解決它到HashSets,LinkedLists和Dictionary,並感謝您的幫助,DJ KRAZE。 – Coolai