我正在编写一个简单的二十一点游戏,并且遇到了将已发牌的String表示附加到String变量DealtCards然后打印它以便我可以看到列表的问题。
必须如下:Ace-Of-Spades
Two-Of-Queen
每次运行我的testDeck应用程序时,它都会一直显示新卡,而不显示最后一张和新的卡。它没有将它添加到String
我们尚未学习数组,所以我不能使用数组来执行此操作
这是我的甲板课程:
public class Deck
{
private Random random;
private String dealtCards;
/**The no-arg constructor simply creates
* a new Random object
*/
public Deck()
{
random = new Random();
dealtCards = "";
}
/** This method will generate random numbers for
* the suit and faceValue variables
* in order to create a card
* @param takes no parameter
* @return randomResult represents a new card object
* generated by random
*/
public Card deal()
{
int suit = 0, faceValue = 0;
suit = random.nextInt(3+1);
faceValue = random.nextInt(13) + 1;
Card randomResult = new Card(suit, faceValue);
dealtCards += randomResult.toString();
return randomResult;
}
public String cardsDealtList()
{
return dealtCards;
}
}
我的卡类:
public class Card
{
private int suit;
private int faceValue;
/**The two parameter Constructor initializes
*the cards suit and facevalue
*@param suit represents the card's suit
*@param faceValue represents the card's face value
*/
public Card(int suit, int faceValue)
{
if(!(suit >= 0 && suit <= 3))
{
suit = 3;
faceValue = 2;
}
this.suit = suit;
this.faceValue = faceValue;
}
/**
*Returns the suit of the card object
*
*@return the suit of the card object
*/
public int getSuit()
{
return suit;
}
/**
*Returns the faceValue of the card object
*
*@return the faceValue of the card object
*/
public int getFaceValue()
{
return faceValue;
}
/**Compare's the current instance to the parameter card
*if they are identical, if one is lower than the other
*or if one if higher than the other
*@param card represents a card object's reference that will
*be passed into it during a compareTo method call
*@return 0 if two cards are identical, 1 if the first card is higher,
* -1 if the first card is lower
*/
public int compareTo(Card card)
{
if(this.getSuit() == card.getSuit() && this.getFaceValue() == card.getFaceValue())
return 0;
else if(this.getSuit() < card.getSuit())
return 1;
else if(this.getSuit() == card.getSuit() && this.getFaceValue() > card.getFaceValue())
return 1;
else
return -1;
}
/** Will return a String representation of the card
*
*@return cardName represents the full name of the card
*/
public String toString()
{
String cardName = null;
switch (faceValue)
{
case 1:
cardName = "Ace";
break;
case 2:
cardName = "Two";
break;
case 3:
cardName = "Three";
break;
case 4:
cardName = "Four";
break;
case 5:
cardName = "Five";
break;
case 6:
cardName = "Six";
break;
case 7:
cardName = "Seven";
break;
case 8:
cardName = "Eight";
break;
case 9:
cardName = "Nine";
break;
case 10:
cardName = "Ten";
break;
case 11:
cardName = "Jack";
break;
case 12:
cardName = "Queen";
break;
case 13:
cardName = "King";
break;
}
switch (suit)
{
case 0:
cardName += "-Of-Spades";
break;
case 1:
cardName += "-Of-Hearts";
break;
case 2:
cardName += "-Of-Diamonds";
break;
case 3:
cardName += "-Of-Clubs";
break;
}
return cardName;
}
}
这是我的应用程序类测试甲板类:
public class TestDeck
{
public static void main(String [] args)
{
Deck deck1 = new Deck();
Card firstCard = deck1.deal();
//System.out.println(firstCard);
System.out.println(deck1.cardsDealtList());
//how to get the list of dealt card???
}
}
答案 0 :(得分:0)
好的,你过度复杂了。您只需多次致电deal
。您甚至不需要将已处理的Card
存储为变量。您的Deck
已经存储了所有已发卡,您只需拨打cardsDealtList()
:
public class TestDeck
{
public static void main(String [] args)
{
Deck deck1 = new Deck();
deck1.deal();
deck1.deal();
System.out.println(deck1.cardsDealtList());
}
}
答案 1 :(得分:0)
您的代码工作正常,但您只需要一张卡。 要获得更多卡片,您需要多次处理
public static void main(String [] args)
{
Deck deck = new Deck();
int cards = 5; // pull 5 cards
for (int i=0 ; i < cards; i++ )
deck.deal();
System.out.println(deck.cardsDealtList());
}
答案 2 :(得分:-1)
public Card deal() {
int suit = 0, faceValue = 0;
suit = random.nextInt(3+1);
faceValue = random.nextInt(13) + 1;
Card randomResult = new Card(suit, faceValue);
dealtCards += dealtCards + randomResult.toString();
return randomResult;
}
通过修改此行ADECards + = dealingCards + randomResult.toString(); 拥有所有已处理的卡片,因为您正在用自己的新卡片替换该String的值。