首页 > 编程语言 > 详细

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

时间:2015-07-28 20:26:49      阅读:278      评论:0      收藏:0      [点我收藏+]
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; 
        } 
    }
}

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

原文:http://www.cnblogs.com/plateFace/p/4683511.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!