我正在创建一个Card类,该类赋予西装价值和等级。我的问题是,即使我的主要方法创建了要测试的新卡,它也只能创建一张卡,并且是唯一可以打印的卡。 我需要的是创建不同的卡来测试它们是否相等。任何关于我的问题所在方法的帮助将不胜感激。对我来说,这是一次学习经历,所以我喜欢指导而不是确切的解决方案。
enum Suit {
HEARTS, DIAMONDS, CLUBS, SPADES
};
enum Rank {
ACE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING
};
public class Card {
private Rank aRank;
private Suit aSuit;
public Card(Suit aSuit, Rank aRank) {
this.aSuit = aSuit;
this.aRank = aRank;
}
public Rank getRank() {
return aRank;
}
public Suit getSuit() {
return aSuit;
}
public String toString() {
String cardValue = "";
String rank = "";
String suit = "";
switch (aSuit) {
case HEARTS:
suit = "hearts";
case DIAMONDS:
suit = "diamonds";
case CLUBS:
suit = "clubs";
case SPADES:
suit = "spades";
}
switch (aRank) {
case ACE:
rank = "Ace";
case TWO:
rank = "2";
case THREE:
rank = "3";
case FOUR:
rank = "4";
case FIVE:
rank = "5";
case SIX:
rank = "6";
case SEVEN:
rank = "7";
case EIGHT:
rank = "8";
case NINE:
rank = "9";
case TEN:
rank = "10";
case JACK:
rank = "Jack";
case QUEEN:
rank = "Queen";
case KING:
rank = "King";
}
cardValue += rank + " of " + suit;
return cardValue;
}
public int compareTo(Card other) {
int rankComparison = aRank.compareTo(other.aRank);
return rankComparison != 0 ? rankComparison : aSuit.compareTo(other.aSuit);
}
public boolean equals(Card other) {
if (aRank == other.aRank)
return true;
if (aSuit == other.aSuit)
return true;
return false;
}
// Main method to test.
public static void main(String[] args) {
Card c1 = new Card(Suit.SPADES, Rank.FIVE);
Card c2 = new Card(Suit.HEARTS, Rank.TWO);
Card c3 = new Card(Suit.CLUBS, Rank.EIGHT);
Card c4 = new Card(Suit.DIAMONDS, Rank.FIVE);
Card r1 = new Card(Suit.CLUBS, Rank.ACE);
Card r2 = new Card(Suit.DIAMONDS, Rank.JACK);
Card r3 = new Card(Suit.HEARTS, Rank.QUEEN);
Card r4 = new Card(Suit.SPADES, Rank.KING);
System.out.println(c1);
System.out.println(c2);
System.out.println(c3);
if (c1.compareTo(c2) < 0)
System.out.println(c2 + " outranks " + c1);
else if (c1.compareTo(c2) > 0)
System.out.println(c1 + " outranks " + c2);
if (c1.compareTo(c3) < 0)
System.out.println(c3 + " outranks " + c1);
else if (c1.compareTo(c3) > 0)
System.out.println(c1 + " outranks " + c3);
if (c1.compareTo(c1) == 0)
System.out.println(c1 + " is equal to " + c1);
else
System.out.println(c1 + " is NOT equal to " + c1);
if (c1.equals(c4))
System.out.println(c1 + " is equal to " + c4);
else
System.out.println(c1 + " is NOT equal to " + c4);
if (r1.compareTo(r2) < 0)
System.out.println(r1 + " comes before " + r2);
else if (r1.compareTo(r2) > 0)
System.out.println(r1 + " comes after " + r2);
else
System.out.println(r1 + " is equal to " + r2);
if (r4.compareTo(r3) < 0)
System.out.println(r4 + " comes before " + r3);
else if (r4.compareTo(r3) > 0)
System.out.println(r4 + " comes after " + r3);
else
System.out.println(r4 + " is equal to " + r3);
}
}
答案 0 :(得分:5)
该错误位于您的toString
方法中。您需要在您的case语句中放置断点,如下所示:
public String toString() {
String cardValue = "";
String rank = "";
String suit = "";
switch (aSuit) {
case HEARTS:
suit = "hearts";
break;
case DIAMONDS:
suit = "diamonds";
break;
case CLUBS:
suit = "clubs";
break;
case SPADES:
suit = "spades";
break;
}
switch (aRank) {
case ACE:
rank = "Ace";
break;
case TWO:
rank = "2";
break;
case THREE:
rank = "3";
break;
case FOUR:
rank = "4";
break;
case FIVE:
rank = "5";
break;
case SIX:
rank = "6";
break;
case SEVEN:
rank = "7";
break;
case EIGHT:
rank = "8";
break;
case NINE:
rank = "9";
break;
case TEN:
rank = "10";
break;
case JACK:
rank = "Jack";
break;
case QUEEN:
rank = "Queen";
break;
case KING:
rank = "King";
break;
}
cardValue += rank + " of " + suit;
return cardValue;
}
答案 1 :(得分:1)
您要创建8张不同的卡,但是您的toString()方法错误,请尝试以下代码:
System.out.println("c1suit: " + c1.getSuit());
System.out.println("c2suit: " + c2.getSuit());
System.out.println("c3suit: " + c3.getSuit());
这表明这些卡的西装不同。
但是什么时候做:
System.out.println(c1)
它将调用toString(),这是您的问题所在。
另请参阅switch statement needs "break"。
出于良好作法,也请添加一个默认值:
default:
suit = "unknown";
}
您将需要修复两个switch语句。
这应该发现您的错误。