如何显示我的原始纸牌 Java

How to Display My Original Deck of Cards Java

洗牌后的牌组会打印出来,但原来的牌组不会,当我使用下面的代码时,我得到了这个:[Ljava.lang.String;@677327b6 它重复了 52 次。

public class Deck {
    public static void main(String[] args) {
    String[] SUITS = {
            "Clubs", "Diamonds", "Hearts", "Spades"
    };

    String[] RANKS = {
            "2", "3", "4", "5", "6", "7", "8", "9", "10",
            "Jack", "Queen", "King", "Ace"
    };

    // initialising deck
    int n = SUITS.length * RANKS.length;
    String[] deck = new String[n];
    for (int i = 0; i < RANKS.length; i++) {
        for (int j = 0; j < SUITS.length; j++) {
            deck[SUITS.length * i + j] = RANKS[i] + " of " + SUITS[j];

     System.out.println("Original Deck Of Cards");
     System.out.println(deck);
    }
 }
    // shuffling the cards in the deck
    for (int i = 0; i < n; i++) {
        int r = i + (int) (Math**strong text**.random() * (n - i));
        String temp = deck[r];
        deck[r] = deck[i];
        deck[i] = temp;
    }
    // printing shuffled deck
    System.out.println("Randomly Shuffled Deck Of Cards");
    for (int i = 0; i < n; i++) {
        System.out.println(deck[i]);
     }

   }
 }

使用Arrays.toString(deck)获取数组的字符串表示形式

改变这个

 for (int i = 0; i < RANKS.length; i++) {
    for (int j = 0; j < SUITS.length; j++) {
        deck[SUITS.length * i + j] = RANKS[i] + " of " + SUITS[j];

 System.out.println("Original Deck Of Cards");
 System.out.println(deck);
}

System.out.println("Original Deck Of Cards");
for (int i = 0; i < RANKS.length; i++) {
    for (int j = 0; j < SUITS.length; j++) {
        deck[SUITS.length * i + j] = RANKS[i] + " of " + SUITS[j];
        System.out.println(deck[SUITS.length * i + j]);
    }
}

希望对您有所帮助

也许你可以使用下面的循环来代替原来的纸牌循环:

        System.out.println("Original Deck Of Cards");
        for (int i = 0; i < RANKS.length; i++) {
            for (int j = 0; j < SUITS.length; j++) {
                deck[SUITS.length * i + j] = RANKS[i] + " of " + SUITS[j];

                System.out.println(deck[SUITS.length * i + j]);
            }
        }

如果您无法使用 Java Class 库中的集合 类 - 编写您自己的方法。类似的东西:

private static String deckToString(String[] deck) {
    StringBuilder template = new StringBuilder();
    for (String card : deck) {
       template.append(card).append("\n");
    }
    return template.toString();
}

然后打印出来:System.out.println(deckToString(deck));