生成二维数组中的随机数

发布于 2024-11-27 06:09:41 字数 395 浏览 0 评论 0原文

我正在使用 Java 创建一个战舰游戏,但我在生成 2 个随机数(随机选择战舰板上的一个位置)时遇到问题。例如,计算机必须在棋盘上随机选择一个空间来放置船只(然后进行射击)。

我创建了一个二维数组:

int rows = 10;
int cols = 10;
char [][] grid;
grid = new char[rows][cols];

然后尝试了几种不同的方法来获取数组中的两个随机数,但我无法让它工作。这是我尝试过的示例:

int randomPos = (char) (Math.random() * (grid[rows][cols] + 1));

如果这没有意义,请问我一些问题。

肖恩

I am creating a game of Battleships using Java and I am having trouble generating 2 random numbers which randomly choose a place on the battleship board. For example, the computer has to randomly choose a space on the board to place the ships (and later to shoot on).

I have created a 2D array:

int rows = 10;
int cols = 10;
char [][] grid;
grid = new char[rows][cols];

And then tried several different ways to get the two random numbers in the array but I cannot get it to work. Here is an example of what I have tried:

int randomPos = (char) (Math.random() * (grid[rows][cols] + 1));

Please ask me some questions if this doesn't make sense.

Sean

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

暮凉 2024-12-04 06:09:41

Math.random() 生成一个十进制数。平方 3.5779789689689... (或您所指的任何内容)没有任何意义,因此请使用 Math.floor() 方法,该方法会四舍五入里面的数字最接近的整数。正如@Sanjay 所解释的,单独生成数字......

int row = (int) Math.floor(Math.random() * rows);
int col = (int) Math.floor(Math.random() * cols);

现在你有了可以使用的实际整数。

Math.random() generates a decimal number. It doesn't make any sense to have square 3.5779789689689... (or whatever you're referring to), so use the Math.floor() method, which rounds the number inside to the nearest integer. And as @Sanjay explained, generate the numbers separately...

int row = (int) Math.floor(Math.random() * rows);
int col = (int) Math.floor(Math.random() * cols);

Now you have actual integers that you can work with.

ペ泪落弦音 2024-12-04 06:09:41

分别生成两个随机数并使用它们来索引您的板。详细说明:

x = random(max_rows)
y = random(max_cols)
(x, y) -> random location (only if it's not already marked)

在您的情况下,随机数的范围应在 0 到 9 之间(包括两者)。

Generate two random numbers separately and use them to index your board. To elaborate:

x = random(max_rows)
y = random(max_cols)
(x, y) -> random location (only if it's not already marked)

In your case, the range of the random number should be between 0 and 9 (inclusive both).

和影子一齐双人舞 2024-12-04 06:09:41
int randomRow = Math.random() * grid.length;
int randomColumn = Math.random() * grid[0].length;

我不会使用 char 作为网格类型,但无论数组中的类型如何,上面的代码都可以正常工作。

int randomRow = Math.random() * grid.length;
int randomColumn = Math.random() * grid[0].length;

I wouldn't use char as my grid type, but the code above works all the same regardless of the types of in the array.

久隐师 2024-12-04 06:09:41

尝试声明这是一个 ivar:

Random random = new Random();

//In the method do this:

int randomPos = generator.nextInt(11); //This generates a number between 0 and 10.

Try this declare this is an ivar:

Random random = new Random();

//In the method do this:

int randomPos = generator.nextInt(11); //This generates a number between 0 and 10.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文