*任何帮助深表感谢 *
我正在使用 java 网站上的类卡示例来尝试构建游戏。
http://java.sun.com/j2se/1.5.0/docs/guide/language/enums.html http://java.sun.com/j2se/1.5.0/docs/guide/language/enums.html
我想分配花色和等级值。我不知道该怎么做..
为了适合,我想做的是指定 Heart = 4
方块 = 3,梅花 = 2,黑桃 = 1
对于排名,Ace = 11
杰克、皇后、国王 = 10
卡的价值为2-10..
该程序接受用户输入作为手数和每手牌数的参数。
像这样: $ 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;
}
}
当我尝试编译时出现错误..
为了
卡.getSuitpoints()
错误:找不到符号:方法 getSuitpoints()
我觉得这很奇怪,因为
卡.getRankpoints()
编译..
这是我将其放入枚举的方式吗?
获取Rankpoints()
返回零。
怎么了?