C#編程基礎 實驗(7) (3)__編程

來源:互聯網
上載者:User

3.假定已經擷取題庫中的試題號,並存放在數組arrayKT中。例如,int[] arrayKT={10,13,18,19,20,22,30,31,···}定義一個靜態成員方法,該方法實現從上述數組中隨機抽出給定數量(n,1<=n<=arrayKT.Length)的考題,並組成一個考題字串。比如隨機從arrayKT中抽取5題組成考題字串:"10,18,20,22,30",要求,組成考題字串中考題不重複,且一定在數組中存在。

代碼如下:

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace program{    class Program    {        static void Main(string[] args)        {           string s=getKTH(5,10,13,18,19,20,22,30,31,33);           Console.WriteLine(s);           Console.ReadKey();        }        public static string getKTH(int n, params int [] arrayKT)        {            string str="";            Random rd = new Random();            int[] num = new int[]{-1,-1,-1,-1,-1};            for (int i = 0; i < 5; i++)            {                int k=rd.Next(arrayKT.Length);                int t = 0;                for (int j = 0; j < 5; j++)                {                    if (k == num[j])                    {                        t = 1;                    }                }                if (t == 1)                {                    i--;                    continue;                }                num[i] = k;                str+=arrayKT[k].ToString();                str += " ";            }            return str;        }    }}


學習心得:

這隻是做了一個特例,而且沒有添加異常處理。。。主要是博主比較懶T.T。。。太久沒使用for搭配continue,居然忘記了i同樣會+1這個問題,害的我找了好一會原因。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.