转置我的数组

时间:2016-11-09 21:49:41

标签: java arrays transpose

现在我有一副卡片可以打印出来:

A of ♠    9 of ♥    6 of ♣    J of ♦    9 of ♦    2 of ♣    9 of ♠    
6 of ♦    2 of ♦    Q of ♠    6 of ♠    5 of ♠    J of ♥    7 of ♦    
K of ♣    4 of ♦    8 of ♣    K of ♥    T of ♥    5 of ♣    A of ♦    
7 of ♣    T of ♠    7 of ♥    T of ♣    A of ♣    8 of ♦    7 of ♠    
2 of ♥    K of ♠    8 of ♠    J of ♠    8 of ♥    J of ♣    4 of ♣    
3 of ♦    5 of ♥    Q of ♦    T of ♦    4 of ♥    Q of ♥    3 of ♣    
K of ♦    2 of ♠    3 of ♥    3 of ♠    4 of ♠    9 of ♣    5 of ♦ 

我想问用户他们的卡在哪一行,然后我想翻转打印的表,使他们的行是一列。

如何区分行以便我可以询问用户,然后如何翻转表格以使其行现在成为列。

我需要转置这个吗?我该怎么做?

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();

    }
}

1 个答案:

答案 0 :(得分:0)

我只想使用你想要的行数和列数制作一个二维数组:private Card[#rows][#columns] deck;。然后你必须重写你的fill,shuffle和toString算法,但这样可以节省你很多时间。

当您想要将行更改为列时,您可以像安迪指出的那样从deck[row][column]更改为deck[column][row]

要询问用户,您只需检查该值是否介于0和deck.length以及0和deck[0].length之间。