public void Random1_100ToArray_Active()
{
List<int> templist = new List<int>();
for (int i = 1; i < 101; i++)
{
templist.Add(i);
}
int [] intarray=new int[100];
int index = 0;
Random r = new Random();
while (templist.Count > 0)
{
int randomindex = r.Next(0,templist .Count);
intarray[index] =templist[ randomindex];
templist.RemoveAt(randomindex);
index++;
}
for (int i = 0; i < intarray.Length; i++)
{
for (int j = i + 1; j < intarray.Length; j++)
{
if (intarray[i] > intarray[j])
{
int temp = intarray[i];
intarray[i] = intarray[j];
intarray[j] = temp;
}
}
}
for (int i = 0; i < intarray.Length; i++)
{
Console.WriteLine(intarray[i]);
}
}
本文介绍了一种生成包含1到100的随机排列整数数组的方法,并通过冒泡排序确保数组有序。该过程首先创建了一个包含1到100的临时列表,随后使用随机数生成器打乱顺序并转换为数组。
2814

被折叠的 条评论
为什么被折叠?



