数组中插入1-100数字,数字不能重复.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            Random random = new Random();
            int[] array = new int[100];

            int i = 0;
            int index = 0;
            while(true)
            {
                i = random.Next(1, 101);
                if(!isExist(array,i))
                {
                    array[index] = i;
                    index++;
                    if (index == 100) 
                    {
                        break;
                    }
                }

            }

            Array.Sort(array);
            foreach (var k in array)
            {
                Console.WriteLine(k);
            }

            Console.ReadKey();
        }

        //判断是否有重复的数字在数组里面
        public static bool isExist(int [] array,int n) 
        {
            for (int i = 0; i < array.Length; i++)
            {
                if (array[i] == n)
                {
                    return true;
                }
             }

            return false; 
        } 
    }
}
posted @ 2015-07-28 17:19 盘子脸 阅读(...) 评论(...) 编辑 收藏