2016-11-09 46 views
0

现在我有打印出像这样的卡的甲板:移调我的数组

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

    } 
} 
+0

只用'multi [j] [i]'交换'multi [i] [j]'。 –

+0

@Andy - 他在这里没有使用多维数组。 – BadZen

+0

@BadZen真的吗?那么这是什么:'String [] [] multi = new String [7] [7];'。 –

回答

0

我只想让甲板上的二维数组你想要的行和列的数量:private Card[#rows][#columns] deck;。然后你必须重写你的填充,洗牌和toString算法,但这会为你节省很多时间。

如Andy所指出的那样,当您想将行更改为列时,您可以从deck[row][column]更改为deck[column][row]

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