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 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog