大家好,我需要一些帮助,我想在 6 个按钮中随机分配 6 个字符串作为文本,而不需要任何重复。那就是我想做某种改组并分发它,但是每个按钮都不会重复,每个按钮都会包含一个唯一的字符串。如果有人可以发布代码,那就太好了:)谢谢
class Card_Deck
{
public Random r;
public string ReceiveCards()
{
List<string> cards = new List<string>();
cards.Add("♣ King");
cards.Add("♦ King");
cards.Add("♥ King");
cards.Add("♠ King");
cards.Add("♣ Jack");
cards.Add("♦ Jack");
int index = r.Next(cards.Count);
var card = cards[index];
cards.RemoveAt(index);
return card;
}
}
}
这是主要形式
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Card_Deck cd = new Card_Deck() { r = new Random(DateTime.Now.Millisecond) };
button1.Text = cd.ReceiveCards();
button2.Text = cd.ReceiveCards();
button3.Text = cd.ReceiveCards();
button4.Text = cd.ReceiveCards();
button5.Text = cd.ReceiveCards();
button6.Text = cd.ReceiveCards();
}
}
}