Java类卡枚举示例。 修订

*任何帮助深表感谢 *

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

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

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

对于套装,我想要做的是分配Heart = 4 diamond = 3,club = 2,spade = 1

排名,ace = 11杰克,女王,国王= 10
2-10卡的价值..

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

所以我想要它打印八个黑桃(8),十个心(40)

基于值…示例spade = 1 * 8 hearts = 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 protoDeck = new ArrayList(); // Initialize prototype deck static { for (Suit suit : Suit.values()) for (Rank rank : Rank.values()) protoDeck.add(new Cards(rank, suit)); } public static ArrayList newDeck() { return new ArrayList(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 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 deal(List deck, int n) { int deckSize = deck.size(); List handView = deck.subList(deckSize - n, deckSize); ArrayList hand = new ArrayList(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; } 

然后,当您想要打印值时,可以调用它。