我的牌组创建了一组 Card 对象,并成功打印出它已更新。我想创建第三个类来实现该程序:创建一个牌组对象,询问玩家他们想要多少张牌,然后随机分发。在底部的 PlayingTable 类中,我尝试创建一个卡片组实例并对其进行扫描,以使卡片组中的所有信息都在那里。它说我无法扫描甲板对象,因为它不是数组,而且我不太确定从这里去哪里....
这是我的两个类的代码:
公共课卡
{
//Instance properties//
private String cardNum;
private String cardSuit;
//Instance Methods //
//Constructor, create an instance of card
public Card( String num , String suit )
{
System.out.println( "Creating a card.");
cardNum = num ;
cardSuit = suit;
}
public void display()
{
System.out.println("This card is a" + " "+ cardNum + cardSuit);
}
}
公共课甲板
{ // 实例属性//
//Number of cards in a deck
public static int numCards = 52;
private static Card [ ] deck= new Card [numCards];
private static String [ ] cardValue = { "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King" } ;
private static String [ ] cardSuit = { " of Hearts" , " of Spades", " of Clubs", " of Diamonds" };
//Instance methods and Constructor//
/*public void Deck ( )
{
}*/
public static void fillDeck ( )
{
int eachCard = 0 ;
for ( int i = 0; i < cardValue.length ; i++ )
{
for ( int j = 0 ; j < cardSuit.length ; j++ )
{
deck[eachCard++] = new Card ( cardValue [i] , cardSuit [j] );
}
}
}
public static void getDeck ()
{
for ( int i = 0; i < numCards ; i++ )
{
fillDeck();
//System.out.println( " This is card" + i + " " + " of the deck" );
deck[i].display();
}
}
public static void main (String [] args )
{
getDeck();
}
}
public class PlayingTable { static Deck DealersDeck = new Deck();
public static void main ( String [] args )
{
for ( int i = 0 ; i < 52; i++)
{
System.out.println ( dealersDeck[i].getDeck());
}
}
}