为什么我的纸牌阵列为空? [英] Why is my array of cards empty?

查看:58
本文介绍了为什么我的纸牌阵列为空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名新手程序员,正在为我的课程学习C#,并且在练习中遇到麻烦,因为我们不得不制作一副纸牌并将它们分发出去。

I am a novice coder learning C# for my class and am having trouble with a practice exercise where we have to make a deck of cards and deal them out.

我们需要创建一个通过构造函数从另一个类接收参数来创建卡的类。然后在Tostring方法中使用这些值来设置带有switch语句的卡的值和适合度,并返回卡。在第二类中,我们用卡片填充数组,然后在Dealing方法中调用Tostring方法,通过将用户从卡片中拉出来随机生成用户指定数量的卡片。

We need to make a class that creates cards by receiving parameters from another class through a constructor. Then use those values in a Tostring method to set the value and suit of those cards with switch statements and return a card. In the second class we populate an array with cards and call the Tostring method in a Dealing method to randomly generate a user specified amount of cards by pulling them out of the array.

问题是我的数组没有被填充,我尝试将它们分配为空后立即对数组的Console.WriteLine部分进行填充。您不必给我完整的答案,只需把我放在正确的轨道上即可。

The problem is my array isn't being populated I have tried to Console.WriteLine parts of the array directly after they would be assigned and they are empty. You don't have to give me the full answer put just put me on the right track.

这里是代码:

这是卡片创建类

 `  class Card
{

    static int value; 
    static int suit;
    static string cdvalue;
    static string cdsuit;

    string card;

    public Card(int ranvalue, int ransuit) //constructor that sets value and suit
    {
        value = ranvalue;
        suit = ransuit;
    }
    public override string ToString()
    {
        switch (value) //switch statement for card value
        {
            case 1: cdvalue = "ace";
                break;
            case 2: cdvalue = "two";
                break;
            case 3: cdvalue = "three";
                break;
            case 4: cdvalue = "four";
                break;
            case 5: cdvalue = "five";
                break;
            case 6: cdvalue = "six";
                break;
            case 7: cdvalue = "seven";
                break;
            case 8: cdvalue = "eight";
                break;
            case 9: cdvalue = "nine";
                break;
            case 10: cdvalue = "ten";
                break;
            case 11: cdvalue = "jack";
                break;
            case 12: cdvalue = "queen";
                break;
            case 13: cdvalue = "king";
                break;
        }
        switch (suit) // switch for card suit
        {
            case 1: cdsuit = "Hearts ";
                break;
            case 2: cdsuit = "Spades ";
                break;
            case 3: cdsuit = "Diamonds ";
                break;
            case 4: cdsuit = "Clubs ";
                break;
        }
        card = cdvalue + " of " + cdsuit;

        return card;// returns a string in the form of "value of suit"`

此类创建套牌

class Deck
{
    Random rng = new Random(); //Random object



    private static Card[] deck;
    static string[] cards; 
    public Deck()
    {
        deck = new Card[52];//creates array of 52 elements
        int l = 0;
        for (int i = 1; i <= 13; i++)//loops to create cards 
        {
            for (int j = 1; j <= 4; j++)
            {

                deck[l++] = new Card(i,j); // populates the array with all 52 cards



            }
        }

    }
    static string dealt;
    static int Rndm;
    public string deal(int number)//parameter received from user
    {
        cards = new string[number];//creates an array to contain dealt card objects
        int m = 0;

        for (int num=0;num<number;num++) // determines the amount of cards to be dealt
        {

            Rndm = rng.Next(0,53);
            cards[m++] = deck[Rndm].ToString();//fills the card array with randomly dealt cards from the deck
            dealt = string.Join(" ", cards); // turns the card array into a single string of the cards dealt



        }
        return dealt;//returns the cards dealt

这是测试类

  static void Main(string[] args)
    {

        // get the number of cards from the user - must be between 1 and 52
        int cardsDealt = -1;
        do
        {
            Console.Write("Enter number of cards to get (1-52): ");
            String dealStr = Console.ReadLine();
            Boolean parsed = int.TryParse(dealStr, out cardsDealt);
        } while (cardsDealt < 1 || cardsDealt > 52);

        // create a Deck object
        Deck cardDeck = new Deck();

        // Call the deal method
        String cards = cardDeck.deal(cardsDealt);

        // List the result
        Console.WriteLine("\nCards dealt:\n" + cards);


推荐答案

在Card类中,变量西装标记为静态

In your Card class you have the variables value and suit marked as static.

private static int value;
private static int suit;

由于这两个变量是静态的,因此将在Card对象的实例之间维护对这些变量的引用。这意味着每次创建新的Card对象时,都会无意中更新所有其他Card对象 value suit 具有相同值的变量。因此,现在无论何时获得Card实例,它都将与所有其他实例(最后创建的Card)相同。

Because these two variables are static, the reference to these variables will be maintained across instances of the Card objects. What this means is that every time you create a new Card object you are inadvertently updating all the other Card objects value and suit variables with the same value. So now whenever you get an instance of a Card it will be the same card as all other instances (the last Card that was created).

从这些实例中删除static关键字声明,您应该可以。

Remove the static keyword from these declarations and you should be ok.

我也建议您阅读static以便更熟悉它的用法: C#中的静态关键字

Also I would suggest reading up on static to more familiarize yourself with its usage: Static keyword in c#

这篇关于为什么我的纸牌阵列为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆