我的程序要求用户输入纸牌数量以及玩家数量。我只是想打印出每张球员的卡片。前任。 (玩家1:黑桃A,红心二。玩家2:梅花二,等等。)我已经打印出来了,但它似乎只打印出一套牌,尽管输入了2张牌。这是我的代码。
手类:
public class Hand
{
private int handSize; //how many cards in the hand
private int cardsInHand; //counter
private Card [] hand;
public Hand ()
{
hand = new Card[52];
handSize = 5;
cardsInHand = 0;
}
public Hand (int handSize)
{
hand = new Card [handSize];
this.handSize = handSize;
}
public void addCard (Card card)
{
if (cardsInHand >= handSize)
{
Card[] temp = new Card[hand.length*2];
for (int i=0; i < cardsInHand; i++)
{
temp[i] = hand[i];
hand = temp;
}
}
}
public class Deck
{
private Card [] deck;
private int nextCard;
public Deck()
{
deck = new Card[52];
int iCardCount=0; // Holds how many cards have been created.
for ( int suit = 0; suit <= 3; suit++ )
{
for ( int face = 1; face <= 13; face++ )
{
deck[iCardCount] = new Card(iCardCount);
iCardCount++;
}
}
nextCard = 0;
}
public Card dealACard ()
{
if (nextCard < 52)
{
System.out.println( deck[nextCard++]);
}
else
{
System.out.print("\nError, out of cards." );
}
return (null);
}
public Hand dealAHand (int n)
{
Hand hand = new Hand(n);
Card deal = new Card(n);
for (int i = 0; i < n; i++)
{
hand = dealACard();
}
return hand;
}
我的司机:
System.out.println ("How many cards in the hand? ");
int iHand = kb.nextInt();
Hand newHand = new Hand(iHand);
System.out.println("How many players are playing? ");
int iPlayers = kb.nextInt();
Deck secondDeck = new Deck();
secondDeck.shuffle();
secondDeck.dealAHand(iHand);
我得到的输出:
How many cards in the hand?
5
How many players are playing?
2
the TWO of SPADES
the ACE of HEARTS
the TEN of DIAMONDS
the QUEEN of CLUBS
the NINE of HEARTS
我希望它打印出两套卡片,而不是只打印一套,但从逻辑上讲,我不知道该怎么做。任何帮助都会很棒,谢谢。
最佳答案
你只有一个 Hand 实例,就像 D.Wallace 所说的那样,你只调用 dealAHand 一次。在开始编码之前,您应该了解解决方案的更多结构。
关于java - 无法为多个玩家创建手牌,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29223617/