单步执行结果输出正确,但执行的时候没结果输出。

卷心菜 2012-09-18 03:04:58
从52张扑克牌中随机抽出5张,然后将5张牌的名字打印。求教了


static void Main(string[] args)
{
Deck myDeck = new Deck();
myDeck.Shufle(); //洗牌,即打乱牌的顺序

Card[] selectedCards = new Card[5];
int[] coCards = new int[5];
for (int i = 0; i < 5;i++ )
{
coCards[i] = i; //初始化
}

//随机抽取0到51之间的5个不重复的数字
for (int i = 0; i < 5;i++ )
{
//检查该数字是否存在于coCards数组
int tempNum = 0;
Boolean exist = false;
Random tempRandom = new Random();

do
{
tempNum=tempRandom.Next(0,52);
for (int j=0;j<i;j++)
{
if (coCards[j]==coCards[i])
{
exist=true;
}
else
{
exist = false;
}
}
} while (exist==true);

if (exist==false)
{
coCards[i]=tempNum;
}
}

//把扑克牌里对应的5张牌取出来
for (int i = 0; i < 5;i++ )
{
selectedCards[i] = myDeck.GetCard(coCards[i]);
}

//显示5张牌
for (int i = 0; i < 5;i++ )
{
Console.WriteLine("{0}",selectedCards[i].ToString());
}

Console.ReadKey();
...全文
124 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
卷心菜 2012-09-18
  • 打赏
  • 举报
回复

public static int[] GetRandom2(int minValue, int maxValue, int count)
{
int[] intList = new int[maxValue];
for (int i = 0; i < maxValue; i++)
{
intList[i] = i + minValue;
}
int[] intRet = new int[count];
int n = maxValue;
Random rand = new Random();
for (int i = 0; i < count; i++)
{
int index = rand.Next(0, n);
intRet[i] = intList[index];
intList[index] = intList[--n];
}


刚才从网上搜索到的一段算法。这最后一句intList[index] = intList[--n];是什么意思呢?
sunyiying 2012-09-18
  • 打赏
  • 举报
回复
问题就是出现在随机数那里,程序执行太快,随机数根本不会变化 。
Louis-Lv 2012-09-18
  • 打赏
  • 举报
回复
先把随机的弄好在说!!!
写代码的小2B 2012-09-18
  • 打赏
  • 举报
回复

const Int32 length = 52;
static void Main(string[] args)
{
Random r = new Random();

//随即生成52个数。
List<Int32> poker = new List<Int32>(length);
for (Int32 i = 1; i <= length; i++)
poker.Add(r.Next(100, 1000));

while (poker.Count > length - 5)
{
int current = poker[r.Next(poker.Count)];
Console.WriteLine(current);
poker.Remove(current);

}

Console.ReadLine();
}

哥们,不晓得为毛这么复杂啊。

bdmh 2012-09-18
  • 打赏
  • 举报
回复
Random tempRandom = new Random();放到for循环外面

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧