xf1327242567 2015-05-06 15:39 采纳率: 100%
浏览 1502
已采纳

C#中关于集合类的一个问题

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace ConsoleApplication1
{
public class Cards
{
Hashtable htCards;
ArrayList randomList;
public Hashtable SetCards() //哈希表添加54张牌
{
htCards = new Hashtable(54, 1);
int i;
string card = "";
for (i = 1; i <= 13; i++)
{

            if (i <= 10)
            {
                card = "红桃" + i.ToString();

            }
            else
            {
                switch (i)
                {
                    case 11: card = "红桃J"; break;
                    case 12: card = "红桃Q"; break;
                    case 13: card = "红桃K"; break;
                }
            }
            htCards.Add(i, card);
        }

        for (i = 14; i <= 26; i++)
        {

            if (i <= 23)
            {
                card = "黑桃" + (i - 13).ToString();

            }
            else
            {
                switch (i)
                {
                    case 24: card = "黑桃J"; break;
                    case 25: card = "黑桃Q"; break;
                    case 26: card = "黑桃K"; break;
                }
            }
            htCards.Add(i, card);
        }

        for (i = 27; i <= 39; i++)
        {
            if (i <= 36)
            {
                card = "方片" + (i - 26).ToString();

            }
            else
            {
                switch (i)
                {
                    case 37: card = "方片J"; break;
                    case 38: card = "方片Q"; break;
                    case 39: card = "方片K"; break;
                }
            }
            htCards.Add(i, card);
        }


        for (i = 40; i <= 52; i++)
        {
            if (i <= 49)
            {
                card = "梅花" + (i - 39).ToString();

            }
            else
            {
                switch (i)
                {
                    case 50: card = "梅花J"; break;
                    case 51: card = "梅花Q"; break;
                    case 52: card = "梅花K"; break;
                }
            }
            htCards.Add(i, card);
        }

        htCards.Add(53, "小王");
        htCards.Add(54, "大王");
        return htCards;
    }
    public ArrayList SendCards()   //发牌,生成13个不重复的随机数
    {
        randomList = new ArrayList();
        Random r = new Random();
        for (int j = 0; j < 13; j++)
        {
            int index;
            index = r.Next(1, 54);
            htCards.Remove(index);
            while (!htCards.Contains (index)||randomList.Contains(index))
            {
                index = r.Next(1, 54);
            }
            randomList.Add(index);

        }
        return randomList;

    }

}
class Program
{
    static void Main(string[] args)
    {
        Cards cards = new Cards();
        Hashtable hxCards = cards.SetCards();


        int count = 0;
        for (int i = 0; i < 2; i++)
        {
             ArrayList indexList = cards.SendCards();
            Console.WriteLine("第{0}位玩家的牌已发,如下:",i+1);
            foreach (int index in indexList)
            {
                if (count % 5 == 0)
                {
                    Console.WriteLine();

                }
                count++;
                Console.Write("{0}\t", hxCards[index]);

            }
            Console.WriteLine();
        }

        Console.ReadLine();
    }


}

}
程序用VS运行出来各种问题!!!各位大神帮我看看。图片说明

  • 写回答

2条回答 默认 最新

  • threenewbee 2015-05-06 15:52
    关注

    hxCards[index]有空白项,调试下。另外几行代码就能写好的代码,你能写这么多,佩服。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题