简体   繁体   English

c#System.NullReferenceException与锯齿状数组

[英]c# System.NullReferenceException with Jagged Array

I have: 我有:

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();
        }
    }
}

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

Is throwing the error in the title. 在标题中引发错误。 I have no clue how to fix this as I am very new to C#. 我不知道如何解决此问题,因为我是C#的新手。

What do I need to do? 我需要做什么?

You have created a jagged array, but it's not a complete array of arrays, it's just an array of null references. 您创建了一个锯齿状的数组,但它不是数组的完整数组,而只是空引用的数组。 You have to create an inner array for each item in the outer array. 您必须为外部数组中的每个项目创建一个内部数组。

You have created an array of five items, but it should be an array that has the length of the number of players. 您已经创建了一个由五个项目组成的数组,但是它应该是一个具有玩家人数长度的数组。 So, you have to create the array after you know how many players there are: 因此,您必须在知道有多少玩家之后创建数组:

...
string[][] hand;

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

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

Now, inside the loop you should create an inner array for each item, that's the array that should have the length five. 现在,在循环内部,您应该为每个项目创建一个内部数组,该数组的长度应为5。 The Random instance should be created outside the outer loop, if you create new instances too close in time they will generate the same number sequences. 应该在外部循环外部创建Random实例,如果您创建的新实例在时间上过于接近,则它们会生成相同的数字序列。

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

As all your inner arrays have the same length, you could use a two dimensional array instead of a jagged array. 由于所有内部数组都具有相同的长度,因此可以使用二维数组而不是锯齿状数组。 It's declared in a similar way: 它以类似的方式声明:

string[,] hand;

and it is created in a similar way: 它以类似的方式创建:

hand = new string[playerCount, 5];

As it is a single array and not an array of arrays, you don't need to create any inner arrays in the loop. 由于它是单个数组,而不是数组数组,因此您无需在循环中创建任何内部数组。

The assignment of items is also slightly different: 项目的分配也略有不同:

hand[currentPlayer, cardNumber] = ...

I have made changes to your code and it runs. 我对您的代码进行了更改,它可以运行。 I believe that is what you would like to achieve 我相信这就是您想要实现的目标

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();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM