C#中获取数组中的随机数

来源:互联网 发布:acdsee mac中文破解版 编辑:程序博客网 时间:2024/05/21 17:49
/*程序的版权和版本声明部分:*Copyright(c)2014,烟台大学计算机学院学生*All rights reserved.*文件名称:*作者:田成琳*完成日期:2014 年 9 月 11 日*版本号:v1.0*对任务及求解方法的描述部分:*问题描述:C#中获取数组中的随机数*程序输入: -*程序输出:随机数组成的字符串*问题分析:*算法设计:*/using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication1{    class Program    {        static void Main(string[] args)        {            Console.WriteLine("请输入题目个数及题号:");            int x = int.Parse(Console.ReadLine());            int[] array = new int[x];            for (int i = 0; i < array.Length; i++)                array[i] = int.Parse(Console.ReadLine());            Console.WriteLine("请输入抽取考题个数:");            int xx = int.Parse(Console.ReadLine());            string s = getKTH(array, xx);            Console.WriteLine("随机抽取题目组成的考题字符串为:{0}", s);            Console.ReadKey();        }        public static string getKTH(int[] a, int n)        {            string s = "";            int x;            Random ran = new Random();            for (int i = 0; i < n; i++)            {                x = ran.Next(0, a.Length - 1);                if (a[x] != -1)                {                    s += a[x];                    a[x] = -1;                }                else                    n++;            }            return s;        }    }}

运行结果:


0 0
原创粉丝点击