我已将程序设置为要求用户输入卡的数量以及玩家的数量。我只想打印出每个玩家卡片。恩。 (玩家1:黑桃王牌,两个红心球员。玩家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
我想要打印出两套卡片而不是一张卡片,但我在逻辑上无法弄清楚如何做到这一点。任何帮助都会很棒,谢谢。
答案 0 :(得分:0)
你只有一个手的实例,就像D.Wallace说的那样,你只召唤过一次。在开始编码之前,您应该研究解决方案的更多结构。