![]() |
||
|
Power Member
![]() ![]() 加入日期: May 2004
文章: 685
|
DEUCE, THREE, FOUR, FIVE, SIX,
SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE 這些是String 還是你自己定義的class |
|||||||
|
|
|
*停權中*
加入日期: Mar 2004
文章: 477
|
定義了Suit, Rank這些variable type,後面method裡也是得回傳object裡的這些variable type.
改完試試看不知有沒好? /*------------------------------------------ public Card(Suit suit, Rank rank, int value) { this.suit = suit; this.rank = rank; this.value = value; } /** * Get the suit of the card * @return String */ public Suit getSuit() { return this.suit; } /** * Get the rank of the card * @return String */ public Rank getRank() { return this.rank; } /** * Get the actual value of the card * @return int */ public int getValue() { return this.value; } } -----------------------------*/ 此文章於 2006-09-28 09:08 AM 被 doberman 編輯. |
||
|
|
|
Major Member
![]() 加入日期: Aug 2001 您的住址: 新竹
文章: 105
|
|
|
|
|
New Member
加入日期: Sep 2006
文章: 9
|
private Rank rank;
private Suit suit; private int value; 這三行就怪怪的 enum類別有final特性 你這樣定義..? 再來 compile error是: Card.java:39: incompatible types found : java.lang.String required: Card.Suit this.suit = suit; ^ Card.java:40: incompatible types found : java.lang.String required: Card.Rank this.rank = rank; ^ Card.java:50: incompatible types found : Card.Suit required: java.lang.String return this.suit; ^ Card.java:59: incompatible types found : Card.Rank required: java.lang.String return this.rank; ^ 4 errors 你的getSuit,getRank return type是String getValue是 int 而enum的值根據書本 不可是int或String 可能可以用valueOf()來取得和名稱相同的值 如Rank.valueOf("DEUCE") 不保證對喔 |
|
|
|
Senior Member
![]() ![]() ![]() 加入日期: Feb 2002 您的住址: 宜蘭
文章: 1,341
|
public class Card {
public enum Rank { DEUCE, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE } public enum Suit { CLUBS, DIAMONDS, HEARTS, SPADES } private Rank rank; // the suit of a card private Suit suit; // the rank of a card private int value; // the actual value of a card /** * Class constructor * The default Card constructor, creates a new Card object with default values. */ public Card() { suit = null; rank = null; value = 0; } /** * Class constuctor * Takes the suit, rank and value the card and create a Card object. * The suit argument must be in either spade, heart, diamond or club. The * value of rank argument must be chosen from Ace to K. The value argument must * be an integer from 1 to 13. * <p> * One Card object will be created with the given parameters. * * @param suit the suit of the card * @param rank the rank of the card * @param value the value of the card */ public Card(String suit, String rank, int value) { this.suit = Suit.valueOf(suit); this.rank = Rank.valueOf(rank); this.value = value; } /** * Get the suit of the card * @return String */ public String getSuit() { return this.suit.toString(); } /** * Get the rank of the card * @return String */ public String getRank() { return this.rank.toString(); } /** * Get the actual value of the card * @return int */ public int getValue() { return this.value; } } 先搞清楚enum 怎麼用~~
__________________
AMD Athlon 64 3000+ Asus A8N-E nfoce 4 empowered Simems DDR 400 512MB *2 Benq 1640 --------------------------------- |
|
|