简体   繁体   中英

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 "." have to be randomized and the "O" has to appear more than once.

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.

Assistance randomizing the "O" and "." and having the "O" appear in random fashion throughout the board which is initialized by the user input via 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) . So, (Math.random() < 100); will always be true. If you want a 50% chance of an O or . 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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