【发布时间】:2014-03-28 02:05:23
【问题描述】:
我一直在寻找问题的答案,但找不到任何解决方案。 我编写了一个程序,用户输入五个学生的姓名、姓氏和社交号码。完成后,用户输入的五个学生中的每一个都会收到一个随机数。但问题是两个学生不能拥有相同的随机数。我知道 1-10000 的机会很小,但这是我的任务。
这是我尝试解决问题但无法正常工作的代码。
while (antal != ggr)
{
string name = Interaction.InputBox("Write name: ");
string lastname = Interaction.InputBox("Write last name: ");
string socialnr = Interaction.InputBox("Write social number: ");
while (a != false)
{
foreach (int item in uniqNum)
{
if (item == _rnd)
{
a = true;
}
else
{
_rnd = rnd.Next(1, 10000);
uniqNum.Add(_rnd);
a = false;
}
}
}
ggr++;
_Studens.Add(new student(name, lastname, socialnr, _rnd));
}
【问题讨论】:
-
我存储随机数的列表..
-
为什么不使用HashSet<int>?
标签: c#