小编Pou*_*pen的帖子

如何将字符串放在2D数组中随机选择的位置

我正在学习Java并在基于网格的thinkssingGame项目上工作,我的loadtargetGrid()方法遇到了一些困难.

我有一个名为Grid的字符串的2D数组,以及我想在六个随机选择的网格位置放置六个符号"X".我想对其他位置什么都不做,默认情况下保留null.

    public class Grid
{
public static final int ROWS      = 5;      // number of rows
public static final int COLUMNS   = 5;      // number of columns
public static final int NUMBER_OF_TARGETS = 6; // number of targets in the grid

private String[][] grid;                // the grid itself, a 2-D array of String
private Random random;                  // random number generator

//Constructor
public Grid()
{
    grid = new String[ROWS][COLUMNS];
    random = new Random();
}

//method
public void loadTargetGrid()
{
      int …
Run Code Online (Sandbox Code Playgroud)

java arrays random

5
推荐指数
1
解决办法
3683
查看次数

标签 统计

arrays ×1

java ×1

random ×1