程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> c#-C#中關於集合類的一個問題

c#-C#中關於集合類的一個問題

編輯:編程綜合問答
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運行出來各種問題!!!各位大神幫我看看。圖片說明

最佳回答:


hxCards[index]有空白項,調試下。另外幾行代碼就能寫好的代碼,你能寫這麼多,佩服。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved