简体   繁体   English

寻求使用Java创建随机数组

[英]Looking to Create a Randomized Array using Java

I'm building out a user defined array as a game board. 我正在将用户定义的数组构建为游戏板。 The characters used "O" and "." 字符使用“ O”和“。” have to be randomized and the "O" has to appear more than once. 必须随机化,并且“ O”必须出现多次。

This is what I have thus far. 到目前为止,这就是我所拥有的。

import java.util.Scanner;


public class PacMan {

    public static void main(String[] args) 
    {


        Scanner input = new Scanner(System.in);
        System.out.println("Input total rows:");
        int row = input.nextInt();
        System.out.println("Input total columns:");
        int column = input.nextInt();



        boolean[][] cookies = new boolean[row+2][column+2];
        for (int i = 1; i <= row; i++)
            for (int j = 1; j <= column; j++);
                cookies [row][column] = (Math.random() < 100);

        // print game
        for (int i = 1; i <= row; i++) 
        {
            for (int j = 1; j <= column; j++)
                if (cookies[i][j]) System.out.print(" O ");
                else             System.out.print(". ");
            System.out.println();
        }
    }
}

The output, for example, produces a 5 x 5 grid, but the "O" only appears once and is at the bottom right of the grid. 例如,输出产生一个5 x 5的网格,但是“ O”仅出现一次并且在网格的右下角。

Assistance randomizing the "O" and "." 协助将“ O”和“。”随机化。 and having the "O" appear in random fashion throughout the board which is initialized by the user input via Scanner. 并在整个电路板上以随机方式显示“ O”,并由用户通过Scanner输入进行初始化。

Here is the updated code which is producing the output that I'm looking for and is user defined. 这是更新的代码,该代码生成我正在寻找的输出并由用户定义。

import java.util.*;
public class PacManTest
{
    public static void main(String[] args)
    {
        char O;
        Scanner input = new Scanner(System.in);
        System.out.println("Input total rows:");
        int row = input.nextInt();
        System.out.println("Input total columns:");
        int column = input.nextInt();

        char board[][] = new char[row][column];

        for(int x = 0; x < board.length; x++)
        {
            for(int i = 0; i < board.length; i++)
            {
                double random = Math.random();
                if(random >.01 && random <=.10)
                {
                    board[x][i] = 'O';
                }

                else {
                    board[x][i] = '.';
                }
                System.out.print(board[x][i] + " ");
            }
            System.out.println("");
        }
    }
}

The main issue is the typo in the first loop: 主要问题是第一个循环中的错字:

cookies [row][column] = (Math.random() < 100);

should be 应该

cookies [i][j] = (Math.random() < 100);

Second, Math.random() returns a value greater than or equal to 0.0 and less than 1.0 (doc) . 其次, Math.random()返回一个大于或等于0.0且小于1.0 (doc)的值 So, (Math.random() < 100); 因此, (Math.random() < 100); will always be true. 永远是真的。 If you want a 50% chance of an O or . 如果您希望有50%的机会获得O或。 use: 采用:

cookies[i][j] = Math.random() < 0.5;

Also, not sure what your motivation is for using a starting index of 1 but array indexes start at 0 . 另外,不确定使用起始索引1而是数组索引从0开始的动机是什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM