嘿伙计们我是这个网站的新手,但是我一直在查看enum
上的大量信息,我有点想出来但可以使用一些帮助。我要做的是让我的卡类中的卡构造函数接受int值并返回与这些值相关联的等级和套装。这就是我在Card课程中使用的内容。
package blackjack;
public class Card {
static Rank rank;
static Suit suit;
public enum Rank{ACE(1),TWO(2),THREE(3),FOUR(4),FIVE(5),SIX(6),SEVEN(7),
EIGHT(8),NINE(9),TEN(10),JACK(11),QUEEN(12),KING(13);
private int code;
public int getCode() {
return code;
}
private Rank(int code) {
this.code = code;
}
public static Rank valueOf(int code) {
for (Rank rank: Rank.values()) {
if (rank.getCode()==code)
return rank;
}
throw new RuntimeException("INVALID CODE");
}
@Override public String toString() {
String s = super.toString();
return s.substring(0, 1) + s.substring(1).toLowerCase();
}
}
public enum Suit{SPADES(1),HEARTS(2),CLUBS(3),DIAMONDS(4);
private int code;
public int getCode() {
return code;
}
private Suit(int code) {
this.code = code;
}
public static Suit valueOf(int code) {
for (Suit suit: Suit.values()) {
if (suit.getCode()==code)
return suit;
}
throw new RuntimeException("INVALID CODE");
}
@Override public String toString() {
String s = super.toString();
return s.substring(0, 1) + s.substring(1).toLowerCase();
}
}
public Card(int rank,int suit) {
this.suit = Suit.valueOf(suit);
this.rank = Rank.valueOf(rank);
}
public static void setRank(Rank rank) {
Card.rank = rank;
}
public static Rank getRank() {
return rank;
}
public static void setSuit(Suit suit) {
Card.suit = suit;
}
public static Suit getSuit() {
return suit;
}
}
在我的主要类中称为BlackJack,我真正想做的就是创建一张新卡并使用我在Card中创建的构造函数的int来分配等级和套装,然后将其输出以确保其正常工作。我不断得到这个我无法弄清楚的疯狂错误。任何帮助都会得到赞赏!这是我的BlackJack /主要课程。
package blackjack;
public class BlackJack {
public static void main(String[] args) {
Card c1 = new Card(1,1);
System.out.println(c1.rank.toString());
}
}
答案 0 :(得分:1)
我建议进行以下编辑:
valueOfAlternate
你可以摆脱代码,这只有当你的卡有一个连续的编号并且你的声明是按顺序时才有效,因为值是0,我们需要从中扣除一个参数
package blackjack;
public class Card {
private final Rank rank;
private final Suit suit;
public enum Rank {
ACE(1),TWO(2),THREE(3),FOUR(4),FIVE(5),SIX(6),SEVEN(7),
EIGHT(8),NINE(9),TEN(10),JACK(11),QUEEN(12),KING(13);
private final int code;
public int getCode() {
return code;
}
private Rank(int code) {
this.code = code;
}
public static Rank valueOf(int code) {
for (Rank rank: Rank.values()) {
if (rank.getCode()==code)
return rank;
}
throw new RuntimeException("INVALID CODE");
}
public static Rank valueOfAlternate(int code) {
return Rank.values()[code-1];
}
@Override public String toString() {
String s = super.toString();
return s.substring(0, 1) + s.substring(1).toLowerCase();
}
}
public enum Suit{
SPADES(1),HEARTS(2),CLUBS(3),DIAMONDS(4);
private final int code;
public int getCode() {
return code;
}
private Suit(int code) {
this.code = code;
}
public static Suit valueOf(int code) {
for (Suit suit: Suit.values()) {
if (suit.getCode()==code)
return suit;
}
throw new RuntimeException("INVALID CODE");
}
public static Suit valueOfAlternate(int code) {
return Suit.values()[code-1];
}
@Override public String toString() {
String s = super.toString();
return s.substring(0, 1) + s.substring(1).toLowerCase();
}
}
public Card(int rank,int suit) {
this.suit = Suit.valueOf(suit);
this.rank = Rank.valueOf(rank);
}
public Card(Rank rank, Suit suit) {
this.suit = suit;
this.rank = rank;
}
public Rank getRank() {
return rank;
}
public Suit getSuit() {
return suit;
}
}
如果没有您的疯狂错误描述,这是我能提供的最好的。
答案 1 :(得分:0)
您可以为枚举提供一个包含多个参数的构造函数。您还可以覆盖方法(如toString
)并在枚举中创建自己的方法(如getValue
)。
所以你的卡片类看起来像这样:
public class Card {
public Enum Suit {
SPADE(0, "Spade"), HEART(1, "HEART"), ... ;
private int code = -1;
private int name = null;
private Suit(int code, String name) {
this.code = code;
this.name = name;
}
public int getValue() { return code; }
@Override
public String toString() { return name; }
}
private Suit suit = null;
private Rank rank = null;
public Card(Suit suit, Rank rank) {
this.suit = suit;
this.rank = rank;
}
public Suit getSuit() { return suit; }
public Rank getRank() { return rank; }
@Override
public String toString() {
return suit.toString() + "-" + rank.toString();
}
}