C#为什么我得到零计数?

时间:2015-07-11 16:18:57

标签: c# string

我希望有人可以提供帮助。我在C#中制作了一副牌,并创建了一个实例,包中没有卡片。我从未在c#中编码,但我很了解java。我很确定这可以在java中工作,所以如果有人能让我知道我哪里出错将会有所帮助。

public class PackOfCards : IPackOfCards
{
    private int cardsInPack;
    private Collection<ICard> pack = new Collection<ICard>();
    public int Count {
        get {
            return this.cardsInPack;
        }
        set {
            this.cardsInPack = value;
        }
    }

    public IEnumerator<ICard> GetEnumerator()
    {
        return pack.GetEnumerator();
    }
    IEnumerator IEnumerable.GetEnumerator()
    { 
        return GetEnumerator();
    }

    public void Create()
    {
        String [] values = {"Ace","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King"};
        String [] suits = {"Hearts", "Diamonds", "Spades", "Clubs"};
        int cardsInPack = 0;
        for(int i = 0 ; i<suits.Length; i++){
            for(int j = 0; j<values.Length; j++, cardsInPack++){
                pack.Add(new Card(values[j], suits[i]));
            }
        }
    }

    public void Shuffle()
    {
        Random num = new Random();

        for (int i = 0; i < pack.Count-1; i++) {
            int ran = num.Next(pack.Count);
            ICard temp = pack.ElementAt(ran);
            pack.RemoveAt(ran);
            pack.Insert(ran, pack.ElementAt(i));
            pack.Insert(i, temp);
        }
    }
    public ICard TakeCardFromTopOfPack()
    {
        int topCard = 0;
        ICard cardRemoved = pack.ElementAt(topCard);
        pack.RemoveAt(topCard);
        return cardRemoved;
    }
}

interface ipackofcards:

public interface IPackOfCards : IReadOnlyCollection<ICard>
{
    void Shuffle ();
    ICard TakeCardFromTopOfPack ();
}

接口Icard:

public interface ICard
{
    String getValue();
    String getSuit();
    String toString();
}

类卡;

public class Card : ICard
{
    private String value;
    private String suit;

    public Card(String v, String s)
    {
        value = v;
        suit = s;
    }
    public String getValue(){
        return value;
    }
    public String getSuit(){
        return suit;
    }
    public String toString(){
        return value+" of  "+suit;
    }
}

interface ipackofcardscreator

public interface IPackOfCardsCreator
{
    IPackOfCards Create ();
}

class packofcardscreator:

public class PackOfCardsCreator : IPackOfCardsCreator
{
    public IPackOfCards Create()
    {
        PackOfCards p = new PackOfCards();
        p.create();  //addition, please note that i have tried this but to no avail
        return p;

    }

}

感谢所有帮助过的人,非常感谢。我将不得不将其作为生命中未解决的问题之一。我相信我所做的一切都是正确的,但也许是其他错误的东西,如IDE或计算机。坏木匠总是责怪他的工具而且我是一个坏木匠。

4 个答案:

答案 0 :(得分:3)

我猜问题是你在调用PackOfCards类的Count属性时得到0。我这样想的原因是你的Create方法的实现。在该方法中,您使用名称cardsInPack声明了新变量,该变量将覆盖cardsInPack字段。该字段用于您的Count属性,当您调用该属性时,您将始终获得0。 尝试更新您的Create方法,如下所示:

public void Create()
{
    String [] values = {"Ace","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King"};
    String [] suits = {"Hearts", "Diamonds", "Spades", "Clubs"};
    this.cardsInPack = 0; //your code is: int cardsInPack = 0;
    for(int i = 0 ; i<suits.Length; i++){
        for(int j = 0; j<values.Length; j++, this.cardsInPack++){
            pack.Add(new Card(values[j], suits[i]));
        }
    }
}

[编辑]

@fsacer的回答还有一件事

public interface IPackOfCards : IReadOnlyCollection<ICard>
{
    void Shuffle ();
    ICard TakeCardFromTopOfPack ();
    public int Count { get; }
}


public interface IPackOfCardsCreator
{
    IPackOfCards Create();
}
public static class PackOfCardsCreator : IPackOfCardsCreator
{
    public static IPackOfCards Create()
    {
        return PackOfCards.Create();
    }
}

然后你可以使用PackOfCardsCreator类来创建新的PackOfCards:

static void Main(string[] args)
{
    IPackOfCards cards = PackOfCardsCreator.Create();
    Console.WriteLine(cards.Count);
    ICard c = cards.TakeCardFromTopOfPack();
    Console.WriteLine(c.toString());
}

答案 1 :(得分:3)

首先,你要从Java中获取太多东西才能工作。其次,如果Colllection类已包含准确的计数,为什么还需要自己的计数器?所以我会按如下方式实现Count属性:

public int Count
{
    get
    {
        return pack.Count;
    }
}

接下来,我认为Create方法应为static并返回该类的新实例:

public static PackOfCards Create()
{
    PackOfCards newPack = new PackOfCards();
    String[] values = { "Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" };
    String[] suits = { "Hearts", "Diamonds", "Spades", "Clubs" };

    for (int i = 0; i < suits.Length; i++)
    {
        for (int j = 0; j < values.Length; j++)
        {
            newPack.pack.Add(new Card(values[j], suits[i]));
        }
    }
    return newPack;
}

在接口中我会用C#来确定方法名称:

public interface ICard
{
    String GetValue();
    String GetSuit();
    String ToString();
}


public class Card : ICard
{
    private String value;
    private String suit;

    public Card(String v, String s)
    {
        value = v;
        suit = s;
    }
    public String GetValue()
    {
        return value;
    }
    public String GetSuit()
    {
        return suit;
    }
    public override String ToString()
    {
        return value + " of " + suit;
    }
}

所以我的Main方法看起来像这样:

static void Main(string[] args)
{
    PackOfCards cards = PackOfCards.Create();
    Console.WriteLine(cards.Count);
    ICard c = cards.TakeCardFromTopOfPack();
    Console.WriteLine(c);
}

另外,我会考虑实施从ICardCard的隐式转换。

答案 2 :(得分:1)

PackOfCardsCreator类有一个名为Create的方法,它返回PackOfCards个对象的实例。创建此对象时,由于尚未定义任何构造函数,因此将执行默认构造函数,并且类的字段将获取其默认值或您指定的值。关于pack

private Collection<ICard> pack = new Collection<ICard>();

您创建了ICard个对象的空集合。但是你会说我已经定义了一个名为Create的方法。是的,你已经这样做了。但是,当有人创建一副卡片时,如下所示:

var packOfCardsCreator = new PackOfCardsCreator();
var packOfCards = packOfCardsCreator.Create();

Create方法的内部,你有这个:

PackOfCards p = new PackOfCards();

我认为你明白了。

  

我该怎么办?

您可以在创建Create对象后调用方法PackOfCards,如下所示:

public IPackOfCards Create()
{
    PackOfCards p = new PackOfCards();
    p.Create();
    return p;
}

答案 3 :(得分:1)

如果您之后正在创建对象PackOfCards的实例并调用Create方法,它应该可以正常工作,至少它对我来说很好看:

Pack of cards