110,538
社区成员
发帖
与我相关
我的任务
分享
宣告一個陣列,利用隨機數產生器Random.Next(),來設計一個程式由電腦產生出1~13的數字共13個,且數字不得重複。
EX:1、6、2、9、11、5、8、10、3、12、7、4、13。
using System;
using System.Collections.Generic;
using System.Text;
namespace Rnd13
{
class Program
{
static void Main(string[] args)
{
Random rnd = new Random();
//1~13取亂數 不重複
int n = 13;
int[] b = new int[13]; //設13個陣列//
while (n != 0)
{
int t = rnd.Next(13); //取亂數//
if (b[t] != -1) //b[t]不等於-1//
{
Console.Write("{0} ", t + 1); //把質列出//
b[t] = -1; //b[t]等於-1就不取//
--n;
}
}
}
}
}