【问题标题】:Appending Strings Together In A Card Game在纸牌游戏中将字符串附加在一起
【发布时间】:2016-02-25 19:44:30
【问题描述】:

我正在编写一个简单的二十一点游戏,但在将发牌的字符串表示附加到字符串变量 DealtCards 然后打印它以便我可以看到列表时遇到了问题。

必须是这样的:Ace-Of-SpadesTwo-Of-Queen

每次我运行我的 testDeck 应用程序时,它都会一直显示新牌,而不会同时显示最后一张和新牌。它没有将它添加到字符串中

我们还没有学过数组,所以我不能用数组来做这个

这是我的甲板课:

public class Deck 
{
    private Random random;
    private String dealtCards;

    /**The no-arg constructor simply creates 
    * a new Random object 
    */
    public Deck()
    {
        random = new Random();
        dealtCards = "";
    }
    /** This method will generate random numbers for 
      * the suit and faceValue variables 
      * in order to create a card 
      * @param takes no parameter 
      * @return randomResult represents a new card object 
      * generated by random
      */
    public Card deal()
    {
        int suit = 0, faceValue = 0;

        suit = random.nextInt(3+1);
        faceValue = random.nextInt(13) + 1;

        Card randomResult = new Card(suit, faceValue);
        dealtCards += randomResult.toString(); 

        return randomResult;

    }
    public String cardsDealtList()
    {
        return dealtCards;
    }
}

我的卡类:

public class Card
{
      private int suit;
      private int faceValue;

    /**The two parameter Constructor initializes
     *the cards suit and facevalue
     *@param suit  represents the card's suit 
     *@param faceValue  represents the card's face value
     */ 
    public Card(int suit, int faceValue)
    {
        if(!(suit >= 0 && suit <= 3))
        {
            suit = 3;
            faceValue = 2;
        }

        this.suit = suit;
        this.faceValue = faceValue;

    }

    /**
     *Returns the suit of the card object 
     *
     *@return the suit of the card object 
     */
    public int getSuit()
    {   
        return suit; 
    }
    /**
     *Returns the faceValue of the card object 
     *
     *@return the faceValue of the card object 
     */
    public int getFaceValue()
    {
        return faceValue;
    }

    /**Compare's the current instance to the parameter card 
     *if they are identical, if one is lower than the other 
     *or if one if higher than the other
     *@param card  represents a card object's reference that will 
     *be passed into it during a compareTo method call
     *@return 0 if two cards are identical, 1 if the first card is higher,
     * -1 if the first card is lower 
     */
    public int compareTo(Card card)
    {   

        if(this.getSuit() == card.getSuit() && this.getFaceValue() == card.getFaceValue())

            return 0;

        else if(this.getSuit() < card.getSuit())

            return 1;

        else if(this.getSuit() == card.getSuit() && this.getFaceValue() > card.getFaceValue()) 

            return 1;

        else 

            return -1;
    }

    /** Will return a String representation of the card 
      *
      *@return cardName represents the full name of the card 
      */
    public String toString()
    {
        String cardName = null;

        switch (faceValue)
        {
            case 1: 
            cardName = "Ace";
            break; 

            case 2:
            cardName = "Two";
            break;

            case 3:
            cardName = "Three";
            break; 

            case 4:
            cardName = "Four";
            break;

            case 5:
            cardName = "Five";
            break;

            case 6:
            cardName = "Six";
            break;

            case 7:
            cardName = "Seven";
            break;

            case 8:
            cardName = "Eight";
            break;

            case 9:
            cardName = "Nine";
            break; 

            case 10: 
            cardName = "Ten";
            break;

            case 11: 
            cardName = "Jack";
            break;

            case 12:
            cardName = "Queen";
            break; 

            case 13: 
            cardName = "King";
            break; 


        }
        switch (suit)
        { 

            case 0:
            cardName += "-Of-Spades";
            break; 

            case 1: 
            cardName += "-Of-Hearts";
            break; 

            case 2:
            cardName += "-Of-Diamonds";
            break;

            case 3:
            cardName += "-Of-Clubs";
            break; 

        }

        return cardName;
    }
}

这是我用来测试 Deck 类的应用程序类:

public class TestDeck
{
    public static void main(String [] args)
    {
        Deck deck1 = new Deck();

        Card firstCard = deck1.deal();
        //System.out.println(firstCard);
        System.out.println(deck1.cardsDealtList());
        //how to get the list of dealt card???

    }
}

【问题讨论】:

  • 您是否尝试过调试和迭代您的代码?
  • 我是一年级学生,我们还没有学会如何调试
  • 你只发一张牌。
  • @user220177 我从来没有被教过如何调试。这是您根据自己的意愿学习的东西。
  • 您的解决方案丢失了......很多。你需要更加努力。我会先让你的套牌保持一系列卡片......其中 52 张。我还会创建一个“经销商”类和一个与之交互的“手”类。谷歌洗牌算法来洗牌。你还有很长的路要走。

标签: java string object concatenation


【解决方案1】:

您的代码运行良好,但您只发了一张牌。 要多发牌,需要多次发牌

public static void main(String [] args)
{
    Deck deck = new Deck();
    int cards = 5;  // pull 5 cards
    for (int i=0 ; i < cards; i++ )
      deck.deal();
    System.out.println(deck.cardsDealtList());
}

【讨论】:

  • 谢谢,看来这是我需要循环才能工作的问题
【解决方案2】:

好吧,你太复杂了。您只需多次致电deal。您甚至不需要将处理的Card 存储为变量。你的Deck已经存储了所有发牌,你只需调用cardsDealtList()

public class TestDeck
{
    public static void main(String [] args)
    {
        Deck deck1 = new Deck();
        deck1.deal();
        deck1.deal();
        System.out.println(deck1.cardsDealtList());

    }
}

【讨论】:

  • 是的,但它并没有把所有的牌都放在那里,每次我再次发牌时都会更换它们。这是一个纸牌游戏,我要处理很多次,直到我没有更多的牌可以处理
  • “保留所有卡片”是什么意思?这将打印 2 张卡片。如果您再次交易,它将打印 3。如果您使用循环,您可以在打印之前定义您想要交易的次数。
【解决方案3】:
public Card deal() {
    int suit = 0, faceValue = 0;

    suit = random.nextInt(3+1);
    faceValue = random.nextInt(13) + 1;

    Card randomResult = new Card(suit, faceValue);
    dealtCards += dealtCards + randomResult.toString();

    return randomResult;

}

通过修改这一行 dealtCards += dealtCards + randomResult.toString(); 拥有所有已发牌,因为您正在用它自己加上新牌替换该字符串的值。

【讨论】:

  • 使用 '+=' 是追加,而不是替换。
  • 您确定您了解+= 的作用吗?
  • += 获取字符串并添加到其中
  • 这就是我想做的,我想追加
猜你喜欢
  • 1970-01-01
  • 2021-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-17
相关资源
最近更新 更多