我创建了一个包含6个数字的数组。我已生成1到40之间的随机数,但每个数字都按顺序显示。我需要它只读6
static void Main()
{
int temp;
int number = 0;
int[] lotto = new int[6];
Random rand = new Random();
for (int i = 0; i <= 40; i++)
{
number = 0;
temp = rand.Next(1, 40);
while (number <= i)
{
if (temp == number)
{
number = 0;
temp = rand.Next(1, 40);
}
else
{
number++;
}
}
temp = number;
Console.WriteLine("your lotto number is " + number);
}
Console.ReadLine();
}
}
}
答案 0 :(得分:0)
试试这个
int number = 0;
int[] lotto = new int[6];
Random rand = new Random();
for (int ctr = 1; ctr <= 6; ctr++)
{
number = rand.Next(1, 41);
while (lotto.Contains<int>(number))
{
number = rand.Next(1, 41);
}
lotto[ctr-1] = number;
Console.WriteLine("Your lotto number is: " + number);
}
Console.ReadLine();
如果你想对它进行排序,你可以像这样做
int[] sortedCopy = lotto.OrderBy(i => i).ToArray();
但是你需要
using System.Linq;
答案 1 :(得分:0)
按Guid.NewGuid()
订购的简单Linq?
var lotto = Enumerable.Range(1, 40)
.OrderBy(i => Guid.NewGuid())
.Take(6)
.ToArray();