c#带有Jagged数组的System.NullReferenceException

时间:2015-11-20 19:51:45

标签: c#

我有:

namespace CardGame
{
    class Dealer
    {
        static void Main(string[] args)
        {
            string[] suits = { "Clubs", "Spades", "Hearts", "Diamonds" };
            string[] specials = { "Jack", "Queen", "King", "Ace" };
            string[][] hand = new string[5][];

            Console.WriteLine("Please enter the number of players:");

            int playerCount = Int32.Parse(Console.ReadLine());

            for (int currentPlayer = 0; currentPlayer < playerCount; currentPlayer++)
            {
                Random rand = new Random();
                for (int cardNumber = 0; cardNumber < 5; cardNumber++)
                {
                    string card;

                    int mode = rand.Next(0, 2);
                    if (mode == 1) // Numeric card...
                    {
                        card = rand.Next(2, 10).ToString();
                    }
                    else // Face card or ace...
                    {
                        card = specials[rand.Next(0, 4)];
                    }
                    hand[currentPlayer][cardNumber] = card += " of " + suits[rand.Next(0, 3)];
                    Console.WriteLine(card += " of " + suits[rand.Next(0, 3)]);
                }
            }
            Console.ReadLine();
        }
    }
}

该行:     hand [currentPlayer] [cardNumber] = card + =“of”+ suit [rand.Next(0,3)];

在标题中抛出错误。我不知道如何解决这个问题因为我对C#很新。

我需要做什么?

2 个答案:

答案 0 :(得分:1)

您已经创建了一个锯齿状数组,但它不是一个完整的数组数组,它​​只是一个空引用数组。您必须为外部数组中的每个项创建一个内部数组。

您创建了一个包含五个项目的数组,但它应该是一个具有玩家数量长度的数组。因此,您必须在知道有多少玩家后创建阵列:

...
string[][] hand;

Console.WriteLine("Please enter the number of players:");
int playerCount = Int32.Parse(Console.ReadLine());

hand = new string[playerCount][];
...

现在,在循环内部,您应该为每个项目创建一个内部数组,这个数组的长度应为5。 Random实例应该在外部循环之外创建,如果你创建新实例的时间太近,它们将生成相同的数字序列。

...
Random rand = new Random();
for (int currentPlayer = 0; currentPlayer < playerCount; currentPlayer++)
{
  hand[currentPlayer] = new string[5];
  ...

由于所有内部数组都具有相同的长度,因此可以使用二维数组而不是锯齿状数组。它以类似的方式宣布:

string[,] hand;

并以类似的方式创建:

hand = new string[playerCount, 5];

由于它是单个数组而不是数组数组,因此您不需要在循环中创建任何内部数组。

项目的分配也略有不同:

hand[currentPlayer, cardNumber] = ...

答案 1 :(得分:0)

我对您的代码进行了更改并运行了。我相信这就是你想要实现的目标

using System.Linq;
namespace CardGame
{
    class Dealer
    {
        static void Main(string[] args)
        {
            string[] suits = { "Clubs", "Spades", "Hearts", "Diamonds" };
            string[] specials = { "Jack", "Queen", "King", "Ace" };

            // Two dimensional arrays - I believe this is what you want to achieve, run the application 
            string[,] hand = new string[5,5];

            Console.WriteLine("Please enter the number of players:");

            int playerCount = Int32.Parse(Console.ReadLine());

            for (int currentPlayer = 0; currentPlayer < playerCount; currentPlayer++)
            {
                Random rand = new Random();
                for (int cardNumber = 0; cardNumber < 5; cardNumber++)
                {
                    string card;

                    int mode = rand.Next(0, 2);
                    if (mode == 1) // Numeric card...
                    {
                        card = rand.Next(2, 10).ToString();
                    }
                    else // Face card or ace...
                    {
                        card = specials[rand.Next(0, 4)];
                    }

                    var temp = " of " + suits[rand.Next(0, 3)];

                    if (card != null && !card.Contains(temp))
                    {
                        hand[currentPlayer, cardNumber] = card += " of " + suits[rand.Next(0, 3)];


                       Console.WriteLine(card += " of " + suits[rand.Next(0, 3)]);

                        //Result
                        Console.WriteLine("Result: {0}", hand[currentPlayer, cardNumber]);
                    }




                }
            }
            Console.ReadLine();
        }
    }
}