![]() |
||
|
Golden Member
![]() ![]() ![]() ![]() 加入日期: Apr 2001
文章: 2,513
|
java 裡的 enum type
要寫一個簡單的撲克牌 class
裡面的 rank 和花色想用 enum 來表示 網上查了一下用法 就開始在 eclipse 著手寫 但是就是錯誤百出 public enum Rank { DEUCE, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE } public enum Suit { CLUBS, DIAMONDS, HEARTS, SPADES } 明明 example 上的 syntax 就是長這樣 但是在 eclipse 裡就是一堆 error 說要 ; Rank 也被畫紅線 又指出沒 enum 這個 type 我已經有 import java.util.*; 但是還是沒有辦法解決 有人能指教一下嗎 感謝! 此文章於 2006-09-27 11:14 PM 被 paradise 編輯. |
|||||||
|
|
|
Master Member
![]() ![]() ![]() ![]() 加入日期: Dec 2001
文章: 1,747
|
也許 您的 Compiler Version 沒有指定好.
Window -> Prefences ->Java -> Compiler ->JDK Compliance .... |
||
|
|
|
Golden Member
![]() ![]() ![]() ![]() 加入日期: Apr 2001
文章: 2,513
|
引用:
應該不是這個問題 因為我連 compile 都還沒開始 只是在寫的時候 eclipse 跳出來的 syntax error |
|
|
|
|
Major Member
![]() 加入日期: Aug 2005 您的住址: 歡迎加入THK
文章: 161
|
也許應該在enum的{}結尾加個分號;...
|
|
|
|
Advance Member
![]() ![]() 加入日期: May 2005
文章: 433
|
引用:
剛剛試了一下上面的程式碼 沒有問題(我也是用eclipse) 能否附上完整的程式碼? |
|
|
|
|
Golden Member
![]() ![]() ![]() ![]() 加入日期: Apr 2001
文章: 2,513
|
引用:
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; this.rank = rank; this.value = value; } /** * Get the suit of the card * @return String */ public String getSuit() { return this.suit; } /** * Get the rank of the card * @return String */ public String getRank() { return this.rank; } /** * Get the actual value of the card * @return int */ public int getValue() { return this.value; } } 才剛開始 還沒寫出甚麼 更別說是 compile 但是光這樣 eclipse 就出現煩人的 syntax error 請大家再幫忙一下 感謝 |
|
|
|
|
Golden Member
![]() ![]() ![]() ![]() 加入日期: Apr 2001
文章: 2,513
|
引用:
這個試過 也是不行 試過把 enum 改成 Enum 就會少一個錯誤 但是其它的還是錯的亂七八遭.. |
|
|
|
|
Master Member
![]() ![]() ![]() ![]() 加入日期: Dec 2001
文章: 1,747
|
你的 JDK 應該是 1.5 或者以後吧 ?
如果不是 ^^; |
|
|
|
Golden Member
![]() ![]() ![]() ![]() 加入日期: Apr 2001
文章: 2,513
|
引用:
是 1.5.0.03 這位同學 別說話說一半啊 ![]() |
|
|
|
|
Advance Member
![]() ![]() 加入日期: May 2005
文章: 433
|
剛剛又把你的程式試了一下
enum宣告依然沒有問題 有問題的是後面像是 this.suit = suit;型別錯誤的地方 看來幫不上什麼忙了,sorry |
|
|