asp.net(c#)随机数生成以及重复数的处理
1个回答
展开全部
private
void
LetMeTry()
{
List<int>
listOne
=
new
List<int>();
List<int>
listTwo
=
new
List<int>();
for
(int
i
=
1;
i
<=
50;
i++)
{//循环添加1-50到list
listOne.Add(i);
listTwo.Add(i);
}
List<int>
intListOne
=
new
List<int>();
List<int>
intListTwo
=
new
List<int>();
Random
r
=
new
Random();
for
(int
i
=
0;
i
<
30;
i++)
{
int
num
=
r.Next(0,
listOne.Count
-
1);
int
tnum
=
listOne[num];
intListOne.Add(tnum);
listOne.Remove(tnum);
}
for
(int
i
=
0;
i
<
30;
i++)
{
int
num
=
r.Next(0,
listTwo.Count
-
1);
int
tnum
=
listTwo[num];
intListTwo.Add(tnum);
listTwo.Remove(tnum);
}
List<int>
intListThree
=
new
List<int>();
foreach
(int
i
in
intListTwo)
{
if
(intListOne.Contains(i))
{//如果包含了都跳过
continue;
}
intListThree.Add(i);
}
foreach
(int
i
in
intListOne)
{
if
(intListTwo.Contains(i))
{//如果包含了都跳过
continue;
}
intListThree.Add(i);
}
//集中两个list
listTwo.Union(intListOne);
foreach
(int
i
in
intListOne)
{
ListBox1.Items.Add(i.ToString());
}
foreach
(int
i
in
intListTwo)
{
ListBox2.Items.Add(i.ToString());
}
foreach
(int
i
in
intListThree)
{
ListBox3.Items.Add(i.ToString());
}
虽然办法有点笨,但还是实现了,我用了3个ListBox把它们分别显示出来了
结果成功
嘿嘿!
void
LetMeTry()
{
List<int>
listOne
=
new
List<int>();
List<int>
listTwo
=
new
List<int>();
for
(int
i
=
1;
i
<=
50;
i++)
{//循环添加1-50到list
listOne.Add(i);
listTwo.Add(i);
}
List<int>
intListOne
=
new
List<int>();
List<int>
intListTwo
=
new
List<int>();
Random
r
=
new
Random();
for
(int
i
=
0;
i
<
30;
i++)
{
int
num
=
r.Next(0,
listOne.Count
-
1);
int
tnum
=
listOne[num];
intListOne.Add(tnum);
listOne.Remove(tnum);
}
for
(int
i
=
0;
i
<
30;
i++)
{
int
num
=
r.Next(0,
listTwo.Count
-
1);
int
tnum
=
listTwo[num];
intListTwo.Add(tnum);
listTwo.Remove(tnum);
}
List<int>
intListThree
=
new
List<int>();
foreach
(int
i
in
intListTwo)
{
if
(intListOne.Contains(i))
{//如果包含了都跳过
continue;
}
intListThree.Add(i);
}
foreach
(int
i
in
intListOne)
{
if
(intListTwo.Contains(i))
{//如果包含了都跳过
continue;
}
intListThree.Add(i);
}
//集中两个list
listTwo.Union(intListOne);
foreach
(int
i
in
intListOne)
{
ListBox1.Items.Add(i.ToString());
}
foreach
(int
i
in
intListTwo)
{
ListBox2.Items.Add(i.ToString());
}
foreach
(int
i
in
intListThree)
{
ListBox3.Items.Add(i.ToString());
}
虽然办法有点笨,但还是实现了,我用了3个ListBox把它们分别显示出来了
结果成功
嘿嘿!
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询