java - 如何用Java洗牌?

标签 java arrays

我一直在尝试洗牌,但输出有错误。我想让它显示一张随机牌,比如黑桃A、两心三王等。但是,结果是如下所示。我如何更改我的代码?

Card@b5f53a
Card@1f6f0bf
Card@137c60d
Card@ab853b
Card@b82368
Card@11c8a71
Card@c53dce
Card@15cda3f
Card@fc9944
Card@1b26af3
Card@8b819f
Card@eb017e
Card@aeffdf
Card@120a47e
Card@f73c1
Card@789144
Card@b5f53a
Card@1893efe
Card@186c6b2
Card@15ee671
Card@eb017e
Card@137c60d
Card@16b13c7
Card@11c8a71
Card@df8f5e
Card@13d93f4
Card@1bca5f1
Card@b82368
Card@329f3d
Card@1749757

代码:

import java.util.Random;

public class deck3 {

    //public int TOTALCARD;
    public Card[] deck;

    public deck3() {
        this.deck = new Card[52];
        String[] suit = {"Spades", "Hearts", "Diamonds", "Clubs"};// Array for
        // suit
        int[] number = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}; // Array

        for (int i = 0; i < suit.length; i++) {
            for (int n = 0; n < number.length; n++) {
                Card c = new Card(suit[i], number[n]); // Card instantiate

                // Using (i*number.length + n) will get you from index 0 to 51
                if (deck[(i * number.length + n)] == null) {
                    deck[(i * number.length + n)] = c; // Accessing index 0 to
                    // 51
                }
            }
        }
    }

    public void CreateDeck() {}

    //int[] TOTALCARD={deck};
    public void shuffleDeck() {

        Random generator = new Random();
        for (int i = 0; i < deck.length; i++) {
            int thisplace = generator.nextInt(50);
            int thatplace = generator.nextInt(50);

            Card savednumber = deck[thisplace];
            deck[thisplace] = deck[thatplace];
            deck[thatplace] = savednumber;
            System.out.println(savednumber);
        }

    }

    public void displayDeck() {
        for (int i = 0; i < deck.length; i++) {
            this.deck[i].display();
            //shuffleDeck();// Calling the display method from my card
            // class.
            //System.out.println(randomNumbers);
        }
    }
}

最佳答案

您需要重写 Card 类中的 public String toString() 方法以获得 pretty-print 效果(而不是 Card@b5f53a,...)。 例如:

class Card {
    private String value;
    private Long value2;

    @Override
    public String toString() {
        return "Card{" +
                "value='" + value + '\'' +
                ", value2=" + value2 +
                '}';
        }
    }

如果你需要随机播放 - 更好的方法是使用 Collections.shuffle()。 例如:

    Card[] cards = new Card[10];
    .... // you cards array (may be you can use List?)
    List<Card> cardList = new ArrayList<Card>();
    Collections.addAll(cardList, cards);
    Collections.shuffle(cardList);
    //transform to array (if you need array)
    cards = cardList.toArray(new Card[cardList.size()]);

如您所见,您可以使用列表而不是数组

关于java - 如何用Java洗牌?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27341530/

相关文章:

java - 如何在 MenuManager 中从 ContributionItems 获取 MenuItems?

java - Java中的参数化@Interface注解

javascript - 如何删除具有匹配值的对象数组中的对象

c# - PHP 数组到 C# 字符串

python - Pandas 更新对随后修改的附加值的更改

java - 如何在不使用父引用节点的情况下在二叉树中找到光标的父节点

java - Selenium 关闭文件选择器对话框

java - Jsoup:从一段javascript中解析html

python - 将数字添加到该数组的特定数据后如何获取数组?

sql - 如何在查询的 WHERE 子句中检查 'any or limited'?