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

/*程序的版权和版本声明部分:
*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
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值