首先要解释我拥有的东西,然后是我想要做的事情。
目前,我的程序使用数组创建了一副牌(我不想要枚举)。然后它将卡片按随机顺序洗牌并打印一张7×7卡片的表格。
我想要做的是能够询问用户他们的卡在哪一行,然后翻转表格,使他们选择的行现在是一列。
如何区分行(如2d数组中),以便我可以询问用户他们的卡在哪一行 和 如何翻转此表以使该行成为列
到目前为止,这是我的代码:
public class Deck {
private Card[] deck;
private int currentCard;
String[][] multi = new String[7][7];
public Deck() {
String[] Faces = { "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A" };
String[] Suits = { "♠", "♣", "♦", "♥" };
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;
}
}
@Override
public String toString() {
String s = "";
int k;
k = 0;
for (int i = 0; i < 7; i++) {
for (int j = 1; j <= 7; j++)
System.out.print(deck[k++] + " ");
System.out.println("");
}
return (s);
}
public static void main(String[] args) {
Deck theDeck = new Deck();
theDeck.shuffle();
theDeck.toString();
}
}
答案 0 :(得分:0)
/
(查找行)和%
(查找行中的列)运算符。toString()
这样的方法。让新方法具有boolean transposed
作为参数。根据值(真/假),您可以以一种或另一种(转置)方式打印卡片 - 保持模型不受影响。