生成0-100中的10个随机数,将10个随机数放入集合中,判断随机数是否为偶数,将所有偶数放到一个新的集合中,分别输出随机集合和偶数随机集合。
时间: 2024-03-01 15:50:41 浏览: 17
好的,以下是一个可能的实现方法:
```csharp
using System;
using System.Collections.Generic;
public class Program {
public static void Main() {
// 生成10个0-100的随机数
List<int> randomList = new List<int>();
Random rand = new Random();
for (int i = 0; i < 10; i++) {
int num = rand.Next(0, 101);
randomList.Add(num);
}
// 打印随机数列表
Console.WriteLine("随机数列表:");
foreach (int num in randomList) {
Console.Write(num + " ");
}
Console.WriteLine();
// 将偶数放入新集合
List<int> evenList = new List<int>();
foreach (int num in randomList) {
if (num % 2 == 0) {
evenList.Add(num);
}
}
// 打印偶数集合
Console.WriteLine("偶数集合:");
foreach (int num in evenList) {
Console.Write(num + " ");
}
Console.WriteLine();
}
}
```
这个程序会生成10个0-100的随机数,并将它们放入一个集合中。然后它会遍历这个集合,判断每个数是否为偶数,将偶数放入一个新的集合中。最后,它会分别打印随机数列表和偶数集合。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)