嗨,大家好,我需要一些帮助,我想在6个按钮中随机分配6个字符串作为文本而不需要重复。这是我想做一些洗牌和分发它,但不会有重复每个按钮将保存一个唯一的字符串。如果任何人都可以张贴代码将是巨大的:)谢谢C#随机分发字符串而不重复
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();
}
}
}
我们不是一个代码工厂?你有什么尝试?你卡在哪里?我们不能帮助那些不会帮助自己的人。 – zellio 2012-07-29 05:52:27
对不起,我不能保存我的代码,但现在我已经如此现在可以请你帮我:) – 2012-07-29 06:03:25