【发布时间】:2012-07-27 06:23:30
【问题描述】:
大家好,我需要一些帮助,我想在 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();
}
}
}
【问题讨论】:
-
我们不是代码工厂?你试过什么?你被困在哪里了?我们无法帮助那些不会自助的人。
-
对不起,我当时无法保存我的代码,但现在我有,所以现在你能帮帮我吗:)