我正在尝试制作卡片技巧,我需要输出一张卡片供用户查看。我知道我必须使用2D数组,但我不知道如何去做。
现在我的甲板类由一系列卡片组成,并将卡片随机顺序洗牌。
public class Deck {
private Card[] deck;
private int currentCard;
public Deck(){
String[] Faces = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"};
String[] Suits = {"Spades", "Clubs", "Diamonds", "Hearts"};
deck = new Card [52];
currentCard = 0;
for (int suit = 0; suit < 4; suit++)
{
for (int face = 0; face < 13; face++){
deck [(face + (suit*13))] = new Card(Suits[suit],Faces[face]);
}
}
}
public void shuffle(){
currentCard = 0;
SecureRandom randomNumber = new SecureRandom();
for (int first = 0; first < deck.length; first++){
int second = randomNumber.nextInt(52);
Card temp = deck [first];
deck[first] = deck [second];
deck[second] = temp;
}
}
public void displayDeck(){
for (Card card : deck)
System.out.println(card);
}
public static void main (String[]args) {
Deck theDeck = new Deck();
theDeck.shuffle();
theDeck.displayDeck();
}
}
我想以表格格式显示卡片:
2颗心7颗钻石3颗心
6个俱乐部K钻石J黑桃
k黑桃2俱乐部Q心
编辑:上面的表只是一个例子,不想输出所有的52张卡