看看我刚写的吧。直接写的。也没写什么方法,怕你看不懂,耦合的地方暂时先忽略不计吧- static void Main(string[] args)
- {
- ArrayList list = new ArrayList();
- #region 初始集合时,往里面加一个偶数
- while (true)
- {
- Random ran = new Random();
- int x = ran.Next(1, 101);
- if (x % 2 == 0)
- list.Add(x);
- break;
- }
- #endregion
- #region 循环添加不同偶数
- while (true)
- {
- if (list.Count == 10)//当集合内数字为10跳出循环
- break;
- Random ran1 = new Random();
- int y = ran1.Next(1, 101);
- if (!list.Contains(y) && (y % 2 == 0))//当集合内不包含这个随机数企鹅这个数字为偶数时添加到集合内
- list.Add(y);
- else
- continue;
- }
- #endregion
- foreach (int i in list)
- {
- Console.WriteLine(i);
- }
- Console.ReadKey();
- }
复制代码 |