Java 类卡片枚举示例。修改

标签 java

*非常感谢任何帮助*

我正在使用来自 java 网站的类卡片示例来尝试构建游戏。

http://java.sun.com/j2se/1.5.0/docs/guide/language/enums.html

我想分配花色和等级值。我不知道该怎么做..

对于西装,我想做的是分配 Heart = 4 方 block = 3,梅花 = 2,黑桃 = 1

对于排名,ace = 11 jack 、皇后、国王 = 10
2-10卡的值(value)..

程序接受用户输入作为手数和每手牌数的参数。 像这样:$ java Deal 4 5

然后我希望它打印黑桃八(8),红心十(40)

基于值.. 例如黑桃 = 1 * 8 红心 = 4 * 10

我可以让它打印手而不是值...

我的例子 电流输出按比例缩小:

FIVE of CLUBS(0),
DEUCE of SPADES(0),
SEVEN of SPADES(0),
TEN of SPADES(0),
THREE of HEARTS(0),
FOUR of SPADES(0),
THREE of DIAMONDS(0),
[TEN of SPADES, THREE of HEARTS, FOUR of SPADES, THREE of DIAMONDS]
[FOUR of CLUBS, FIVE of CLUBS, DEUCE of SPADES, SEVEN of SPADES]
[QUEEN of HEARTS, SIX of HEARTS, FOUR of HEARTS, KING of DIAMONDS]

C:\Java\a02>

这是程序在两个不同类中的代码

import java.util.*;

public class Cards {

    public enum Rank {
        DEUCE(2), THREE(3), FOUR(4), FIVE(5), SIX(6), SEVEN(7), EIGHT(8), NINE(
                9), TEN(10), JACK(10), QUEEN(10), KING(10), ACE(11);

        private int Rankpoints;

        Rank(int points) {
            this.Rankpoints = points;
        }

        public int getRankpoints() {
            return this.Rankpoints;
        }

    }

    public enum Suit {
        CLUBS(2), DIAMONDS(3), HEARTS(4), SPADES(1);

        private int Suitpoints;

        Suit(int points) {

            this.Suitpoints = points;

        }

        public int getSuitpoints() {
            return this.Suitpoints;
        }

    }

    private final Rank rank;
    private final Suit suit;

    private Cards(Rank rank, Suit suit) {
        this.rank = rank;
        this.suit = suit;
    }

    public Rank rank() {
        return this.rank;
    }

    public Suit suit() {

        return this.suit;

    }

    public String toString() {
        return rank + " of " + suit;
    }

    private static final List<Cards> protoDeck = new ArrayList<Cards>();

    // Initialize prototype deck
    static {
        for (Suit suit : Suit.values())
            for (Rank rank : Rank.values())
                protoDeck.add(new Cards(rank, suit));

    }

    public static ArrayList<Cards> newDeck() {

        return new ArrayList<Cards>(protoDeck); // Return copy of prototype deck
    }

}

这是主要的

import java.util.*;

public class Deal {
    public static void main(String args[]) {
        int numHands = Integer.parseInt(args[0]);
        int cardsPerHand = Integer.parseInt(args[1]);
        List<Cards> deck = Cards.newDeck();
        Collections.shuffle(deck);

        for (Cards card : deck) {
            System.out.println(card.rank() + " of " + card.suit() + "("
                    + card.getSuitpoints() + ")" + ",");

        }

        for (int i = 0; i < numHands; i++)
            System.out.println(deal(deck, cardsPerHand));
    }

    public static ArrayList<Cards> deal(List<Cards> deck, int n) {
        int deckSize = deck.size();
        List<Cards> handView = deck.subList(deckSize - n, deckSize);
        ArrayList<Cards> hand = new ArrayList<Cards>(handView);
        handView.clear();
        return hand;
    }
}

当我尝试编译时出现错误.. 对于

card.getSuitpoints()

错误:找不到符号:方法 getSuitpoints()

我觉得很奇怪,因为

card.getRankpoints() 编译.. 这是我将其放入枚举的方式吗?

getRankpoints() 返回零。 怎么了?

最佳答案

您在卡片组中看到重复项的原因是您对卡片进行了两次迭代。

for (Cards suit : deck) // ********* here is where i print the deck ********
{
    for (Cards rank : deck) {
        System.out.println(rank.rank() + " of " + suit.suit() + ",");
    }
}

假设您的牌组生成器正常工作,迭代一次就足够了:

for (Cards card : deck)
{ 
    System.out.println(card.rank() + " of " +  card.suit() + ",");  
} 

此外,Card 对于表示卡片的类来说可能是比 Cards 更好的名称选择。

要获取该值,您需要添加一个返回它的方法。

Rank(int points)
{
    this.Rankpoints = points;  
}

public int getRankpoints() {
    return this.Rankpoints;
}

然后你可以在你想打印值的时候调用它。

关于Java 类卡片枚举示例。修改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2151232/

相关文章:

java - MappedSuperclass - 在子类中更改 SequenceGenerator

java - java中的@Override

java - 错误 :cannot get automation extension for chrome with selenium

java - 为什么 if 语句会左右摇晃屏幕

java - 打印从 1 到 x 的所有可能选项

java - 在另一个 JFrame 中调用 JFrame

java - 在java中填充列表的列表

java - 在 Java 中将未检查的异常包装为已检查的异常

java - 包括来自导入的 maven 库的依赖项

java - 尝试将 ObjectInputStream 的输出保存到文件时出现 IOException