简体   繁体   中英

Why is this array out of bounds?

class Program
{
    static void Main(string[] args)
    {
        string[] deck = {"1♥","2♥","3♥","4♥","5♥","6♥","7♥","8♥","9♥","10♥","11♥","12♥","13♥",
                     "1♦","2♦","3♦","4♦","5♦","6♦","7♦","8♦","9♦","10♦","11♦","12♦","13♦",
                     "1♣","2♣","3♣","4♣","5♣","6♣","7♣","8♣","9♣","10♣","11♣","12♣","13♣",
                     "1♠","2♠","3♠","4♠","5♠","6♠","7♠","8♠","9♠","10♠","11♠","12♠","13♠"};

        string[] player = new string[26];
        string[] computer = new string[26];

        deck = Shuffle(deck);

        foreach (string d in deck)
        {
            Console.WriteLine(d);
        }

        Console.WriteLine(deck.Length);

        for (int i = 0; i < 26; i++)
        {
            player[i] = deck[i];
            Console.WriteLine(player[i]);
        }

        for (int j = 26; j < 52; j++)
        {
            computer[j] = deck[j];
            Console.WriteLine(computer[j]);
        }


    }

    static string[] Shuffle(string[] deck)
    {
        Random r = new Random();

        for (int i = deck.Length; i > 0; i--)
        {
            int j = r.Next(i);
            string k = deck[j];
            deck[j] = deck[i - 1];
            deck[i - 1] = k;
        }
        return deck;
    }

}

So what I tried doing was making a deck of cards. Then what I did was use the Shuffle method to shuffle the array and change the deck array.

It will distribute the half of the deck array to both the player and the computer (player gets first half computer gets second half). Now this is shuffled first so yeah it may seem fair.

So the line I'm getting an out of bounds error is this line:

computer[j] = deck[j];

You need to start indexing computer from 0, not 26:

for (int j = 26; j < 52; j++)
{
    computer[j - 26] = deck[j];
    Console.WriteLine(computer[j - 26]);
}

Alternatively, offset the deck index:

for (int i = 0; i < 26; i++)
{
    computer[i] = deck[i + 26];
    Console.WriteLine(computer[i]);
}

If you didn't need to print the initial cards, you could have achieved this much more concisely using LINQ:

player = deck.Take(26).ToArray();
computer = deck.Skip(26).ToArray();

Your computer array length is 26 therefore max index is 25. but you are trying to add element to 26. index in that loop,that is the reason of the error:

for (int j = 26; j < 52; j++)
{
     computer[j] = deck[j];
     Console.WriteLine(computer[j]);
}

And the solution is subtract 26 from j in here:

computer[j - 26] = deck[j];

Also after that line:

Console.WriteLine(computer[j-26]);

应该是computer[j-26] = deck[j];

Because computer is only length 26 , however, the j starts at 26 and goes up to 52 .

You need to use an offset of 26 when indexing computer ,

for (int j = 26; j < 52; j++)
{
    computer[j - 26] = deck[j];

    Console.WriteLine(computer[j]);
} 

In your loop, you're index is correct for deck since it is large enough. However, computer is only 26 elements long. Your i starts at 26, which is beyond the end of computer . You'll need to adjust one way or the other for the two arrays, see below

for (int j = 26; j < 52; j++)
{
    computer[j-26] = deck[j];
    //        ^ <-adjust for smaller array
    Console.WriteLine(computer[j]);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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