Java - 扑克牌游戏输出问题

时间:2015-05-05 05:39:41

标签: java

我坚持为什么我的班级没有以正确的格式打印。当四只手中的每只手应该由不同的牌组成时,它只为每只手打印相同的Card对象。

2 个答案:

答案 0 :(得分:2)

一个可运行的例子会很好,但是从快速阅读中我认为你的if语句实际上应该是if-then-else,例如。

//If there is a suit with 3 cards or more, return 0 points.
if ( c >=3 || d >= 3 || s >=3 || h >=3) {
    retVal = 0;
}  
//If there is a suit with 2 cards, return 1 points.
else if ( c == 2 || d == 2 || s == 2 || h == 2) {
    retVal = 1;
} 
//If there is a suit with 1 card, return 2 points.
else if ( c == 1 || d == 1 || s == 1 || h == 1) {
    retVal = 2;
}     
//If there is a suit with 0 cards, return 3 points.
else {
    retVal = 3;
}        

注意最后的else - 因为你已经覆盖了大于0的所有内容,所以你最后不需要测试0。

修改

您的代码可以清理很多。代码越干净,就越容易阅读。一些建议:

<强>评论

您有很多评论,但是您正在使用这些评论来补偿错误的变量名称!例如,

//Variable to hold number of cards that contain the club suit.
int c = 0;

这很好,但是稍后在代码中使用c时呢?这不是不言自明的。首先有意义地命名变量是非常有用的。

int clubs = 0;

<强>枚举

不要使用字符作为套装,而是使用枚举。它更好地体现了意义,并减少了打字错误的可能性,就像你在测试字符串/字符时一样。

public enum Suit
{
    CLUBS,
    DIAMONDS,
    HEARTS,
    SPADES
}

对于每个循环

除非你需要索引变量,否则每个循环都更容易阅读。

 for (int i = 0; i < theHand.length; i++) {
     ...
 }

可以改写为

for (Card card : cards) {
    ...
}

缩小范围

在需要时声明变量,而不是之前 - 这会减少它们的范围,从而减少误用它们的机会。例如,

public int countDistributionPoints() {
    //Variable to hold the number of distribution points.
    int retVal = 0;

    // a whole bunch of stuff that doesn't use retVal

    //If there is a suit with 3 cards or more, return 0 points.
    if ( c >=3 || d >= 3 || s >=3 || h >=3) {
        retVal = 0;
    }  
    //etc

    return retVal;
}

您可以通过将retVal移动到使用它的位置来缩小retVal的范围。

public int countDistributionPoints() {
    // a whole bunch of stuff that doesn't use retVal

    //Variable to hold the number of distribution points.
    int retVal = 0;

    //If there is a suit with 3 cards or more, return 0 points.
    if ( c >=3 || d >= 3 || s >=3 || h >=3) {
        retVal = 0;
    }  
    //etc

    return retVal;
}

将它们放在一起

重写您的Hand课程,以及一些switch语句和重命名的变量会为我们提供此信息。

public class Hand {
    private final Card[] cards;

    public Hand(Card[] cards) {
        this.cards = cards;
    }

    /**
     * Looks through each Card in the hand array and
     * adds its points (if it has any) to a sum.
     * @return the sum of the hand
    */
    public int countHighCardPoints() {
        int points = 0;
        for (Card card : cards) {
            points += card.getPoints();
        }
        return points;
    }

    /**
     * Count the number of Cards in each suit:
     * a suit with 3 cards or more counts for zero points
     * a suit with 2 cards counts one point (this is called a doubleton)
     * a suit with 1 card counts 2 points (this is called a singleton)
     * a suit with 0 cards counts 3 points (this is called a void)
     * @return the sum of the points
    */
    public int countDistributionPoints() {
        int clubs = 0;
        int diamonds = 0;
        int spades = 0;
        int hearts = 0;

        for (Card card : cards) {
            switch (card.getSuit()) {
                case CLUBS:
                    clubs++;
                    break;
                case DIAMONDS:
                    diamonds++;
                    break;
                case SPADES:
                    spades++;
                    break;
                case HEARTS:
                    hearts++;
                    break;
            }
        }

        final int result;
        if (clubs >= 3 || diamonds >= 3 || spades >= 3 || hearts >= 3) {
            result = 0;
        }
        else if (clubs == 2 || diamonds == 2 || spades == 2 || hearts == 2) {
            result = 1;
        }
        else if (clubs == 1 || diamonds == 1 || spades == 1 || hearts == 1) {
            result = 2;
        }
        else {
            result = 3;
        }

        return result;
    }

    public String toString() {
        String club = "";
        String diamond = "";
        String heart = "";
        String spade = "";

        for (Card card : cards) {
            switch (card.getSuit()) {
            case CLUBS:
                club.append(card.toString().replace(",", " "));
                break;
            case DIAMONDS:
                diamond.append(card.toString().replace(",", " "));
                break;
            case HEARTS:
                heart.append(card.toString().replace(",", " "));
                break;
            case SPADES:
                spade.append(card.toString().replace(",", " "));
                break;
        }
    }

    //Concatenates all of the string values of the clubs, diamonds, hearts, and spades into one string.
    return club + "\n" + diamond + "\n" + heart + "\n" + spade;
}

}

答案 1 :(得分:-1)

你需要再次声明对象

classname objectname = new classname();

INSIDE正在制造手中的东西