dimanche 17 avril 2016

Placing Random Characters on an Array Board in Java

I'm making a simple array board. I'm trying to make the character '$' appear 10 times in random places, and 'R' appear only once in random places. I've continued to change the randomization, but I know I'm approaching this the wrong way. The characters appear in random places and in random amounts. Here is my code:

import java.util.*;
public class board
{
    public static void main(String[] args)
    {
        char $;
        char R;
        char board[][] = new char[10][10];

        for(int x = 0; x < board.length; x++)
        {
            for(int i = 0; i < board.length; i++)
            {
                double random = Math.random();
                if(random <=.10)
                {
                    board[x][i] = 'R';
                }
                else if(random <= .10)
                {
                    board[x][i] = '$';
                }
                else {
                    board[x][i] = '.';
                }
                System.out.print(board[x][i] + " ");
            }
            System.out.println("");
        }
    }
}




Aucun commentaire:

Enregistrer un commentaire