2016-08-10 62 views
1

我正在使用c#winforms。當我點擊一個按鈕時,程序會顯示列表中的2個單詞,但有時單詞會重複。如何我不給不讓的話重複千萬不要重複一些名字

這是代碼

private void button1_Click(object sender, EventArgs e) 
{ 
    var words = new[] { "Worm", "Fast", "Death", "boat", "Sneak", "Destroction" }; 
    var wordsInRandomOrder = words.OrderBy(i => Guid.NewGuid()); 

    foreach (var word in wordsInRandomOrder) 
    { 
     textBox1.Text = word; 
     break; 
    } 

    foreach (var word in wordsInRandomOrder) 
    { 
     textBox1.Text = textBox1.Text + " " + word; 
     break; 
    } 
} 
+2

爲什麼不'textBox1.Text = string.Join(「」,wordsInRandomOrder.Take(2));'? – juharr

+0

@juharr我是新的c# – Pedro

+3

任何時候你有一個循環,你總是在* first *迭代之後中斷,因此你可能不需要循環。 – David

回答

1

選項1:

private void button1_Click(object sender, EventArgs e) 
{ 
    var words = new[] { "Worm", "Fast", "Death", "boat", "Sneak", "Destroction" }; 
    var wordsInRandomOrder = words.OrderBy(i => Guid.NewGuid()).ToList(); 
    textBox1.Text = wordsInRandomOrder[0] + " " + wordsInRandomOrder[1]; 
} 

選項2:

var words = new[] { "Worm", "Fast", "Death", "boat", "Sneak", "Destroction" }; 
Random rd = new Random(); 

int firstIndex = rd.Next(0, words.Length); 
int secondIndex = rd.Next(0, words.Length); 

while (secondIndex == firstIndex) 
{ 
    secondIndex = rd.Next(0, words.Length); 
} 

textBox1.Text = words[firstIndex] + " " + words[secondIndex]; 
+0

它不工作 – Pedro

+0

@HimBromBeere你是對的 – Pedro

+0

@HimBromBeere是的,你是絕對正確的xD – mjb

0
Random rd = new Random(); 

int firstIndex = rd.Next(0, words.Length); 
int secondIndex = rd.Next(0, words.Length); 

while (secondIndex == firstIndex) 
{ 
    secondIndex = rd.Next(0, words.Length); 
} 

textBox1.Text = words[firstIndex] + " " + words[secondIndex]; 
+0

我在哪裏放名單? 「Worm」,「Fast」,「Death」,「boat」,「Sneak」,「Destroction」 – Pedro

+0

在這行前面聲明一個字符串數組「Random rd = new Random()」,就像你在你的代碼:var words = new [] {「Worm」,「Fast」,「Death」,「boat」,「Sneak」,「Destroction」}; – mjb

+0

對不起,但juharr已經幫助我,但thx無論如何 – Pedro

0

一類似的方法,不會使用Guid

private void button1_Click(object sender, EventArgs e) 
{ 
    Random r = new Random(); 
    var words = new[] { "Worm", "Fast", "Death", "boat", "Sneak", "Destroction" }; 
    var wordsInRandomOrder = words.OrderBy(i => r.Next()).ToList(); 

    textBox1.Text = String.Join(" ", wordsInRandomOrder.Take(2)); 
} 

隨着甚至如果隨機數發生器產生兩次相同的數,你從你的列表中取一個字,因爲他們只是下令這appraoch(這是相當不可能的)。如果兩件商品具有相同的訂單價值,則它們可以任意訂購,但同一件商品不能使用兩次。

+0

只能使用'r.Next()',因爲該值僅用於排序而不是索引。 – juharr

+0

@juharr是的,你是對的,更新 – HimBromBeere

0

如果realllllly想保持你使用,在第二foreach的代碼,你可以這樣做:

foreach (var word in wordsInRandomOrder) 
{ 
    if(word != textBox1.Text) 
    { 
     textBox1.Text = textBox1.Text + " " + word; 
     break; 
    } 
} 

,但更好的辦法是爲juharr寫道:

textBox1.Text = string.Join(" ", wordsInRandomOrder.Take(2)); 

如果你想走循環路線,最好加入 這兩個「foreach es」你有這樣的樣子:

foreach (string word in wordsInRandomOrder) 
{ 
    if(textBox1.Text = "") 
    { 
     textBox1.Text = word; 
    } 
    else 
    { 
     if(textBox1.Text != word) 
     { 
      textBox1.Text = textBox1.Text + " " + word; 
      break; 
     } 
    } 
} 
+0

thx的幫助,但mjb做得很好 – Pedro