Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在二维数组中的随机点上放置一个数字_Java_Arrays_Random - Fatal编程技术网

Java 在二维数组中的随机点上放置一个数字

Java 在二维数组中的随机点上放置一个数字,java,arrays,random,Java,Arrays,Random,我有一个2D数组,有5行5列。我想让它在2D数组中的8个随机点(让它选择一个随机的行和列)放置一个字符“1” 我所做的是调用Random类并生成一个介于0和4之间的数字(对于数组中的5个点),然后我有两个for循环运行8次(对于我想要的8个随机点),一个穿过行,另一个穿过列 这是我目前掌握的代码: char[][] battleship = new char[5][5]; //I didn't include this but I have a for loop that populates a

我有一个2D数组,有5行5列。我想让它在2D数组中的8个随机点(让它选择一个随机的行和列)放置一个字符“1”

我所做的是调用Random类并生成一个介于0和4之间的数字(对于数组中的5个点),然后我有两个for循环运行8次(对于我想要的8个随机点),一个穿过行,另一个穿过列

这是我目前掌握的代码:

char[][] battleship = new char[5][5];
//I didn't include this but I have a for loop that populates all the rows and columns with a char of '0'
        Random random = new Random();
        int randomNum = random.nextInt(4);

        for (int i = 0; i < 8; i++)
        {
              for (int o = 0; o < 8; o++)
        {

            battleship[randomNum][randomNum] = '1';

        }
        }
char[][]战舰=新char[5][5];
//我没有包括这个,但我有一个for循环,它用字符“0”填充所有行和列
随机=新随机();
int randomNum=random.nextInt(4);
对于(int i=0;i<8;i++)
{
对于(int o=0;o<8;o++)
{
战列舰[randomNum][randomNum]=“1”;
}
}
我遇到的问题是,它不是把“1”放在8个随机点上,而是把5个点连在一起

我如何纠正这个问题

以下是一个输出示例:

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1. 1. 1. 1. 1. 0 0 0 0 0

“1”不在8个随机点中


我哪里出错了?

在循环之前,你会得到一个随机数,所以它永远不会改变。基本上,
randomNum
变量被滚动并赋值一次-您应该多次调用
nextInt
方法。试试这个:

    for (int i = 0; i < 8; i++) {
        int randomX = random.nextInt(battleship.length);
        int randomY = random.nextInt(battleship[randomX].length);
        battleship[randomX][randomY] = '1';
    }
for(int i=0;i<8;i++){
int randomX=random.nextInt(战列舰长度);
int randomY=random.nextInt(战列舰[randomX].length);
战列舰[randomX][randomY]=“1”;
}
请注意,这并不能解决碰撞问题-您可能很不幸多次获得相同的位置,并且只填充1-7个点

nextInt(int)
的文档中:

返回0到0之间的伪随机、均匀分布的int值 (包括)和指定值(不包括),从中提取 随机数发生器的序列


让嵌套循环运行8次,每个循环将迭代64次。这样做不需要嵌套循环。其中一个简单的方法是使用while循环并分配8个随机点,直到取下所有8个点:

int occupiedSpots = 0;
Random random = new Random();

while(occupiedSpots < 8){
    int x = random.nextInt(array.length);
    int y = random.nextInt(array[0].length);
    if(battleship[x][y] == 0){
        battleship[x][y] = 1;
        occupiedSpots++;
    }
}
int-occupiedSpots=0;
随机=新随机();
而(占用点<8){
int x=random.nextInt(array.length);
int y=random.nextInt(数组[0].length);
if(战列舰[x][y]==0){
战列舰[x][y]=1;
占用点++;
}
}
还要确保在每次迭代中生成新的随机数,否则将始终使用相同的随机值


使用while循环还可以确保所有8个点位于不同的位置。如果只使用for循环而不进行检查,则有一种趋势,即某些点可能两次落在同一位置。

我将采用不同的方法。如果假设5x5 2D数组实际上是一个长25个元素的一维数组,那么基本上只需要生成8个介于0和25之间的不同数字

你的代码也不能保证8个随机数都是不同的

试试这个:

    for (int i = 0; i < 8; i++) {
        int randomX = random.nextInt(battleship.length);
        int randomY = random.nextInt(battleship[randomX].length);
        battleship[randomX][randomY] = '1';
    }
    // Initialize random number array
    int[] positions = new int[25];
    for (int i = 0; i < 25; i++) {
        positions[i] = i;
    }

    char[][] battleship = new char[5][5];

    // Fill the battleship field
    for (int i = 0; i < 8; i++) {
        int random = (int)(Math.random() * (25 - i - 1));
        int position = positions[random];
        positions[random] = positions[25 - i - 1];
        int row = position / 5;
        int col = position % 5;
        battleship[row][col] = '1';
    }

    // Show the field
    for (int row = 0; row < 5; row++) {
        for (int col = 0; col < 5; col++) {
            System.out.print(battleship[row][col] + " ");
        }
        System.out.println();
    }
//初始化随机数数组
int[]位置=新int[25];
对于(int i=0;i<25;i++){
位置[i]=i;
}
战舰=新战舰[5][5];
//填满战场
对于(int i=0;i<8;i++){
int random=(int)(Math.random()*(25-i-1));
int位置=位置[随机];
位置[随机]=位置[25-i-1];
int row=位置/5;
int col=位置%5;
战列舰[row][col]=“1”;
}
//展示场地
对于(int行=0;行<5;行++){
for(int col=0;col<5;col++){
系统输出打印(战列舰[行][列]+“”);
}
System.out.println();
}

是的,但这并不能解释打印方法中的连续1s错误。我想这段代码实际上存在一些问题。随机。nextInt(4)将从0-3生成随机值。但是OP有5行5列。另一个问题是在同一位置获得多个1的可能性。@user3437460没错,我只关注生成多个随机值的问题,并复制了一些原始代码。我不会对您的实际问题进行评论,因为您在下面发布了一些有效的解决方案,但是您应该使for循环计数和数组大小匹配。现在你的
battleship
数组有25个元素,但你正在尝试循环64个元素。我不相信发布的代码可以创建包含这些内容的数组。根据你发布的代码,你应该有一个“1”。如何打印阵列?你有更多的行动吗?