Java 如何向二维数组中的随机块添加特定值?

Java 如何向二维数组中的随机块添加特定值?,java,arrays,random,Java,Arrays,Random,我要做的是给数组中的两个随机点分配一个数字(如2)。下面是我如何设置阵列的 public static void print_array(int[][] array) { for (int row = 0; row < array.length; row++) { System.out.print("\n-----------------\n" + "| "); for (int col = 0;

我要做的是给数组中的两个随机点分配一个数字(如2)。下面是我如何设置阵列的

public static void print_array(int[][] array) {
    for (int row = 0; row < array.length; row++) {
        System.out.print("\n-----------------\n" +
                         "|  ");
        for (int col = 0; col < array[row].length; col++) {
            if (col != array[row].length) {
                System.out.print(array[row][col] + "|  ");
            if (col == array[row].length) {
                System.out.print("\n");
                }
            }
        }
    }
    System.out.print("\n-----------------");
}

如何将整数放入数组中的随机点?

可以使用模运算符。此运算符返回剩余值。 例如

假设有两个随机数num1和num2,就知道二维数组的维数是MxN。即M行和N列。 所以当你这么做的时候

num1 % M returns a number between 0...M
num2 % N returns a number between 0...N
这使您能够执行以下操作:

array[num1%M][num2%N]=2;

根据评论编辑: 如果您正在生成随机数,则:

Random randomGenerator=new Random();
int num1 = randomGenerator.nextInt(array.length);
int num2 = randomGenerator.nextInt(array[0].length);
在这种情况下,您可以根据长度获得随机数,以便直接使用它

array[num1][num2]=2;
您可以尝试以下方法:

public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {

        int[][]array={{3,4,5},{1,8,3},{33,7,5}};
        print_array(array);
        placeBlock(array,2);
        print_array(array);


    }
    public static void placeBlock(int[][] array,int value) {
        Random rand=new Random();
        int randRow=rand.nextInt(array.length);
        int randCol=rand.nextInt(array[0].length);
        array[randRow][randCol]=value;
    }
    public static void print_array(int[][] array) {
        for (int row = 0; row < array.length; row++) {
            System.out.print("\n-----------------\n" +
                             "|  ");
            for (int col = 0; col < array[row].length; col++) {
                if (col != array[row].length) {
                    System.out.print(array[row][col] + "|  ");
                if (col == array[row].length) {
                    System.out.print("\n");
                    }
                }
            }
        }
        System.out.print("\n-----------------");
    }

}
公共类测试{
/**
*@param args
*/
公共静态void main(字符串[]args){
int[][]数组={{3,4,5},{1,8,3},{33,7,5};
打印数组(数组);
placeBlock(阵列,2);
打印数组(数组);
}
公共静态void placeBlock(int[][]数组,int值){
Random rand=新的Random();
int randRow=rand.nextInt(array.length);
int randCol=rand.nextInt(数组[0].length);
数组[randRow][randCol]=值;
}
公共静态无效打印数组(int[][]数组){
for(int row=0;row
这可能有助于解释如何获得两个随机数:P.@Zéychin我把它留给OP,因为它可能是其他函数的输出。但它会根据您的评论进行编辑!检查我的答案,希望这是您想要的。非常感谢,但它只在[0][2]和[2][3]处打印。我不这么认为。只需运行4-5次,然后检查我是否将种子设置为
1
,而不是
Random()
。它工作得很好。谢谢
array[num1][num2]=2;
public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {

        int[][]array={{3,4,5},{1,8,3},{33,7,5}};
        print_array(array);
        placeBlock(array,2);
        print_array(array);


    }
    public static void placeBlock(int[][] array,int value) {
        Random rand=new Random();
        int randRow=rand.nextInt(array.length);
        int randCol=rand.nextInt(array[0].length);
        array[randRow][randCol]=value;
    }
    public static void print_array(int[][] array) {
        for (int row = 0; row < array.length; row++) {
            System.out.print("\n-----------------\n" +
                             "|  ");
            for (int col = 0; col < array[row].length; col++) {
                if (col != array[row].length) {
                    System.out.print(array[row][col] + "|  ");
                if (col == array[row].length) {
                    System.out.print("\n");
                    }
                }
            }
        }
        System.out.print("\n-----------------");
    }

}