困惑于如何为数组中的每一列生成唯一的随机数

时间:2019-06-14 02:03:25

标签: java

我正在编写一个程序来为班级分配创建一个宾果游戏,但是我对如何在5x5数组中的每一列使用不同的数字有些困惑。我现在的代码正在为数组创建随机数,但是某些运行在列中具有相同的数字。任何帮助将不胜感激。

下面是前两列的一些编码。

public static void newCard() {
        System.out.println("B" + "  " + "I" + "  " + "N" + "  " + "G" 
            + "  " + "O");
        int card [][] = new int[5][5];
        for (int i = 0; i < card.length; i++) {
            for (int j = 0; j < card[i].length; j++) { 
                    card[i][0] = (int)(Math.random() * 15 + 1);
                     if (card[i][0] == card[i][j]) {
                    card[i][0] = (int)(Math.random() * 15 + 1);
            card[i][1] = (int)(16 + Math.random() * 15);
                 if (card[i][1] == card[i][j]) {
                card[i][1] = (int)(16 + Math.random() * 15);
                    }
//New Code 
public static void main(String[] args) {
        int[][] card = newCard();
        System.out.println("B  I  N  G  O");
        for (int i = 0; i < card.length; i++) {
            for (int j = 0; j < card[i].length; j++) {
                System.out.printf("%2d ", card[j][i]);
            }
            System.out.println();
        }
        Scanner input = new Scanner(System.in);
        while (calledCard()) {
        System.out.println("Enter the called number: ");
        int number = input.nextInt();
        }
    }
public void calledCard(int number) {
        for(int i = 0; i <= 4; i++) {
            for(int j = 0; j <= 4; j++) {
                if(newCard()[i][j]==number) {
                    newCard()[i][j] = 0;
                }  else  {
                    continue;
                }
            }
        }

    }
The results I get are allowing each column to have some of the same numbers shown.

2 个答案:

答案 0 :(得分:1)

信息:

  

典型的宾果游戏使用数字1到75。纸牌的五列从左到右分别标记为“ B”,“ I”,“ N”,“ G”和“ O”。中心空间通常标记为“自由”或“自由空间”,并被视为自动填充。卡上可以显示的打印数字范围通常受列限制,“ B”列仅包含1到15之间的数字,“ I”列仅包含16到30,“ N”列包含31到45 ,“ G”包含46至60,“ O”包含61至75。

您可以使用Collections.shuffle将每个列表随机化,这是使用方法:

     public static void newCard() {
        List<List<Integer>> nums = new ArrayList<>();
        int k=1;
        for (int i = 0; i <5; i++) {
            nums.add(new ArrayList<>());
            for(int j=0; j<15; j++){
                nums.get(i).add(k++);
            }
//            System.out.println(nums.get(i));
        }


        System.out.println("B" + "  " + "I" + "  " + "N" + "  " + "G"
                + "  " + "O");
        int card [][] = new int[5][5];
        for (int i = 0; i < card.length; i++) {
            Collections.shuffle(nums.get(i));
        }

        for (int i = 0; i < card.length; i++) {
            for (int j = 0; j < card[i].length; j++) {
                card[i][j] = nums.get(j).get(i);
                //System.out.print(card[i][j]+" ");
            }
            //System.out.println("");
        }
//        System.out.println(card);
    }

答案 1 :(得分:-1)

由于每列都有一个有效的值范围(B为1-15,I为16-30,N为31-45,G为46-60,O为61-75),因此您可以创建包含有效范围的列表,随机将其洗牌,然后从列表中选择前五个值以填充宾果卡列。

下面是代码生成的数组,其值在[begin,end]范围内:

public static int[] rangeClosed(int beginInclusive, int endInclusive) {
    if (beginInclusive > endInclusive) {
        throw new IllegalArgumentException("Begin must be less than or equal to end for range.");
    }
    int[] range = new int[endInclusive - beginInclusive + 1];
    for (int i = 0; i < range.length; i++) {
        range[i] = beginInclusive + i;
    }
    return range;
}

这是在数组中交换两个值的代码:

public static void swap(int[] array, int index1, int index2) {
    if (index1 < 0 || index1 >= array.length) {
        throw new IllegalArgumentException("Swap index one is out of bounds for the given array.");
    } else if (index2 < 0 || index2 >= array.length) {
        throw new IllegalArgumentException("Swap index two is out of bounds for the given array.");
    }
    int tmp = array[index1];
    array[index1] = array[index2];
    array[index2] = tmp;
}

这里的代码通过遍历数组并随机地将每个索引与另一个索引交换来随机调整数组:

public static void shuffle(int[] array) {
    Random rand = new Random();
    for (int i = 0; i < array.length; i++) {
        swap(array, i, rand.nextInt(array.length));
    }
}

最后,这是随机生成宾果卡的代码以及显示它的主要功能:

public static int[][] randomBingoCard() {
    int bingoRange = 15;
    int bingoCardSize = 5;
    int[][] card = new int[bingoCardSize][bingoCardSize];
    for (int i = 0; i < card.length; i++) {
        int[] columnRange = rangeClosed(i * bingoRange + 1, i * bingoRange + bingoRange); 
        shuffle(columnRange);
        for (int j = 0; j < card[i].length; j++) {
            card[i][j] = columnRange[j];
        }
    }
    return card;
}

public static void main(String[] args) {
    int[][] card = randomBingoCard();
    System.out.println("B  I  N  G  O");
    for (int i = 0; i < card.length; i++) {
        for (int j = 0; j < card[i].length; j++) {
            System.out.printf("%2d ", card[j][i]);
        }
        System.out.println();
    }
}

编辑:

对于您要填写的新问题,我提供了3个新功能以及一个更新的主功能。一种功能是通过检查是否已调用所有数字来控制循环,一种功能是在调用时标记一个数字,而另一种功能是打印出当前卡。

public static boolean allNumbersMarked(int[][] card) {
    for(int i = 0; i < card.length; i++) {
        for(int j = 0; j < card[i].length; j++) {
            if(card[i][j] != 0) {
                return false;
            }
        }
    }
    return true;
}

public static void markNumber(int[][] card, int number) {
    for(int i = 0; i < card.length; i++) {
        for(int j = 0; j < card[i].length; j++) {
            if(card[i][j] == number) {
                card[i][j] = 0;
            }
        }
    }
}

public static void printCard(int[][] card) {
    System.out.println("B  I  N  G  O");
    for (int i = 0; i < card.length; i++) {
        for (int j = 0; j < card[i].length; j++) {
            System.out.printf("%2d ", card[j][i]);
        }
        System.out.println();
    }
}

public static void main(String[] args) {
    int[][] card = randomBingoCard();
    printCard(card);
    Scanner input = new Scanner(System.in);
    while (!allNumbersCalled(card)) {
        System.out.println("Enter the called number: ");
        int number = input.nextInt();
        markNumber(card, number);
        printCard(card);
    }
    input.close();
}

由于要添加新方法,因此使BingoCard对象为您执行此操作很有意义。在您只需要一张卡之前,一个简单的静态函数和main就足够了,但是现在值得将该程序做成面向对象程序。