【发布时间】:2016-02-25 19:44:30
【问题描述】:
我正在编写一个简单的二十一点游戏,但在将发牌的字符串表示附加到字符串变量 DealtCards 然后打印它以便我可以看到列表时遇到了问题。
必须是这样的:Ace-Of-SpadesTwo-Of-Queen
每次我运行我的 testDeck 应用程序时,它都会一直显示新牌,而不会同时显示最后一张和新牌。它没有将它添加到字符串中
我们还没有学过数组,所以我不能用数组来做这个
这是我的甲板课:
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;
}
}
这是我用来测试 Deck 类的应用程序类:
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???
}
}
【问题讨论】:
-
您是否尝试过调试和迭代您的代码?
-
我是一年级学生,我们还没有学会如何调试
-
你只发一张牌。
-
@user220177 我从来没有被教过如何调试。这是您根据自己的意愿学习的东西。
-
您的解决方案丢失了......很多。你需要更加努力。我会先让你的套牌保持一系列卡片......其中 52 张。我还会创建一个“经销商”类和一个与之交互的“手”类。谷歌洗牌算法来洗牌。你还有很长的路要走。
标签: java string object concatenation