PCDVD數位科技討論區

PCDVD數位科技討論區 (https://www.pcdvd.com.tw/index.php)
-   七嘴八舌異言堂 (https://www.pcdvd.com.tw/forumdisplay.php?f=12)
-   -   java 裡的 enum type (https://www.pcdvd.com.tw/showthread.php?t=657909)

paradise 2006-09-27 11:13 PM

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.*;
但是還是沒有辦法解決

有人能指教一下嗎 感謝!

chlang 2006-09-27 11:48 PM

也許 您的 Compiler Version 沒有指定好.

Window -> Prefences ->Java -> Compiler ->JDK Compliance ....

paradise 2006-09-27 11:57 PM

引用:
作者chlang
也許 您的 Compiler Version 沒有指定好.

Window -> Prefences ->Java -> Compiler ->JDK Compliance ....


應該不是這個問題
因為我連 compile 都還沒開始
只是在寫的時候 eclipse 跳出來的 syntax error

誰找我 2006-09-28 12:09 AM

也許應該在enum的{}結尾加個分號;...

MOSWU 2006-09-28 12:27 AM

引用:
作者paradise
應該不是這個問題
因為我連 compile 都還沒開始
只是在寫的時候 eclipse 跳出來的 syntax error

剛剛試了一下上面的程式碼
沒有問題(我也是用eclipse)

能否附上完整的程式碼?

paradise 2006-09-28 01:55 AM

引用:
作者MOSWU
剛剛試了一下上面的程式碼
沒有問題(我也是用eclipse)

能否附上完整的程式碼?


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
請大家再幫忙一下 感謝

paradise 2006-09-28 01:58 AM

引用:
作者誰找我
也許應該在enum的{}結尾加個分號;...


這個試過 也是不行
試過把 enum 改成 Enum 就會少一個錯誤
但是其它的還是錯的亂七八遭..

chlang 2006-09-28 02:47 AM

你的 JDK 應該是 1.5 或者以後吧 ?

如果不是 ^^;

paradise 2006-09-28 03:05 AM

引用:
作者chlang
你的 JDK 應該是 1.5 或者以後吧 ?

如果不是 ^^;


是 1.5.0.03

這位同學 別說話說一半啊 :cry: :cry:

MOSWU 2006-09-28 08:32 AM

剛剛又把你的程式試了一下
enum宣告依然沒有問題
有問題的是後面像是 this.suit = suit;型別錯誤的地方

看來幫不上什麼忙了,sorry


所有的時間均為GMT +8。 現在的時間是09:36 AM.

vBulletin Version 3.0.1
powered_by_vbulletin 2025。