程序生成50个随机数(介于1到10之间)并告知小于5且大于5的值(C#)
答案 0 :(得分:1)
试试这个:
int n = 50
IList<int> randomNumbers = new List<int>(n);
Random ran = new Random(1);
for (int i = 0; i < n; i++)
{
randomNumbers.Add(ran.Next(1, 10));
}
int lessThan5Count = randomNumbers.Count(c => c < 5);
int greaterThan5Count = randomNumbers.Count(c => c > 5);
答案 1 :(得分:0)
你可以这样做:
Random r = new Random();
int n = 50;
int smaller_than_5 = 0;
int bigger_than_5 = 0;
double[] d = new double[n];
for (int i = 0; i < n; i++)
{
d[i] = 1 + r.NextDouble() * 9;
if (d[i] < 5) smaller_than_5++;
else if (d[i] > 5) bigger_than_5++;
}
希望这会有所帮助......