Java 我如何打印出一个随机的二维数组而不重复?

Java 我如何打印出一个随机的二维数组而不重复?,java,arrays,Java,Arrays,我的CompSci课程有一个作业,我们正在做,我们必须打印出一副二维的卡片,6行8列的数组。每张卡基本上是随机生成的数字1-12和随机选择的套装钻石、红桃、黑桃和梅花;阵列中的任何位置都不能重复任何卡。这是我的密码: static Random random = new Random(1234567); static int i = 1; static int a; static int d; static List<String> suits = new LinkedList

我的CompSci课程有一个作业,我们正在做,我们必须打印出一副二维的卡片,6行8列的数组。每张卡基本上是随机生成的数字1-12和随机选择的套装钻石、红桃、黑桃和梅花;阵列中的任何位置都不能重复任何卡。这是我的密码:

static Random random = new Random(1234567);

static int i = 1;

static int a;
static int d;

static List<String> suits = new LinkedList<String>();

static List<String> cards = new LinkedList<String>();

static int[][] grid = new int[6][8];

public static void main(String[]args)
{
    suits.add("Diamonds");
    suits.add("Clubs");
    suits.add("Hearts");
    suits.add("Spades");

    cards.add("1");
    cards.add("2");
    cards.add("3");
    cards.add("4");
    cards.add("5");
    cards.add("6");
    cards.add("7");
    cards.add("8");
    cards.add("9");
    cards.add("10");
    cards.add("11");
    cards.add("12");

    drawGrid();
}
private static void drawGrid()
{
    for(int b = 0; b < grid.length; b++)
    {
        for(int c = 0; c < grid[i].length; c++)
        {
            a = (int)(Math.floor(suits.size() * Math.random()));
            d = (int)(Math.floor(suits.size() * Math.random()));

            System.out.print(" |" + cards.get(d) + " " + suits.get(a) + "|");

            Collections.shuffle(suits);
            Collections.shuffle(cards);
        }
        System.out.println();
    }
}

有不同的方法可以做到这一点。我的第一个想法是创建一个int数组列表

    ArrayList<int[]> list = new ArrayList<int []> ();

    for (int i=0; i<suits.size(); i++)
    {
        for(int j=0; j<cards.size(); j++)
        {
            int[] card = {i,j};
            list.add(card);
        }
    }

    Collections.shuffle(list);

现在每个元素代表一张卡片。使用前6个元素,您将获得6张随机卡片。元素[0]=suit和元素[1]=cardnumber创建一个变量,用于记住是否已绘制卡片

static boolean[][] drawn = new boolean[4][13];
然后用以下内容替换您绘制卡片的位:

do {
    a = (int)(Math.floor(suits.size() * Math.random()));
    d = (int)(Math.floor(cards.size() * Math.random()));
} while (drawn[a][d]);
drawn[a][d] = true;
最后,删除正在洗牌两个集合的两行。Math.random将为您提供一张随机卡

        Collections.shuffle(suits);
        Collections.shuffle(cards);

你能做的就是把你以前选过的卡片存放在某个地方,这样你就不会有重复的卡片了。我相信这将有助于:

创建一个集合,将其称为selectedCardMap:

对于一个会给你一张新的未使用的卡片的方法,让我们调用这个方法getNewCard,在里面,创建你想要得到的相同的随机数,但是这次,在我们得到它之后,我们检查卡片是否被选中,如果被选中,然后重复检查,直到我们得到一张未被选中的卡片,如下所示:

public static String getNewCard() {

while (true) {
    int a = (int)(Math.floor(suits.size() * Math.random()));
    int d = (int)(Math.floor(suits.size() * Math.random()));

    //Make a key for the map
    String key= d+"-"+a;


    //check if it was selected
    if (!selectedCardMap.contains(key)) {
       //This card is available, add it to selected and return it!
       selectedCardMap.add(key);
       return " |" + cards.get(d) + " " + suits.get(a) + "|";     
    }
  }
}

在这之后,继续你所做的一切!,对不起,如果有任何代码错误,我只是写了这个没有编译它。但这是代码的主要思想。希望您理解并对您有用。

当前代码有什么问题?它起作用了吗?让你知道每件衣服有13件。为什么你有一个静态的随机对象,然后使用Math.Random everywhere?主题要求使用Set。不要重新发明轮子。@Shad0w720z您真的需要使用int数组吗。正如Attila已经提到的,您的示例注定要使用一些面向对象的解决方案。
a = (int)(Math.floor(suits.size() * Math.random()));
d = (int)(Math.floor(suits.size() * Math.random()));
public static String getNewCard() {

while (true) {
    int a = (int)(Math.floor(suits.size() * Math.random()));
    int d = (int)(Math.floor(suits.size() * Math.random()));

    //Make a key for the map
    String key= d+"-"+a;


    //check if it was selected
    if (!selectedCardMap.contains(key)) {
       //This card is available, add it to selected and return it!
       selectedCardMap.add(key);
       return " |" + cards.get(d) + " " + suits.get(a) + "|";     
    }
  }
}