有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何设置带有随机字符的2D字符数组?

我正在解决一个问题,我试图获得一个百分比的字符,并使用Math在5x5数组中随机填充它们。random可在二维数组中存储25项。我现在已经想到了这个,但是我不明白为什么它没有填充我想要的字符。它只是在做‘X’,也不是随机的,它似乎与X的总数是一致的。我试图输出X的一个百分比,O,以及任何剩下的空白。它只输出10个X,它们似乎不是随机的。我希望能得到任何关于为什么会发生这种情况的建议

public static void main(String []args){

        char [][] tissue = new char [5][5];

        assignCellTypes(tissue,40,40);

    }

    public static void assignCellTypes(char[][] tissue,int percentO, int percentX){


                double cellx=0.0;
                double cellO=0.0;
                double totalO=0;
                double totalX = 0;
                totalO = (double)percentO/100;


                totalX = (double)
                        percentX/100;

                        cellx = totalX *(tissue.length*tissue.length);
                        cellO = totalO *(tissue.length*tissue.length);

            int i;
            int j ;
        for(int row = 0;row<tissue.length;row++){
            for(int col = 0;col<tissue[row].length;col++){


                    if(cellx>0){
                        i = (int)Math.floor(Math.random()*tissue.length);
                        j = (int)Math.floor(Math.random()*tissue.length);

                        tissue[i][j] = 'X';
                        System.out.print(tissue[i][j] + "  ");

                        cellx--;

                        if(cellO>0 && tissue[i][j] != 'X' ){
                            i = (int)Math.floor(Math.random()*tissue.length);
                            j = (int)Math.floor(Math.random()*tissue.length);

                            tissue[i][j] = 'O';
                            System.out.print(tissue[i][j] + "  ");
                            cellO--;

                    if(tissue[i][j] != 'X' && tissue[i][j] != 'O'){

                        tissue[i][j] = ' ';

                        System.out.print(tissue[i][j] + "  ");

                    }
                        }



                    }


            }
            System.out.println();
        }


    }
}

共 (1) 个答案

  1. # 1 楼答案

    我不知道你想归档什么,但似乎你只是想用XO和一个偶然因素随机填充一个字符数组,在这种情况下,有一个更简单的解决方案

    public static void assignCellTypes(char[][] tissue, int percentO, int percentX)
    {
        double chanceO = percentO / (double) ( percentO + percentX );
    
        Random r = new Random(); //java.util.Random
    
        for(int i=0; i < tissue.length; i++)
            for(int j=0; j < tissue[i].length; j++)
                if( r.nextDouble() <= chanceO )
                    tissue[i][j] = 'O';
                else
                    tissue[i][j] = 'X';
    }
    

    编辑@comment,我想这就是你想要实现的:

    public static void assignCellTypes(char[][] tissue, int percentO, int percentX)
    {
        if(percentO + percentX > 100)
            throw new Exception("You can't have more than 100%!");
    
        double chanceO = percentO/100d;
        double chanceX = percentX/100d;
    
        Random r = new Random(); //java.util.Random
    
        for(int i=0; i < tissue.length; i++)
            for(int j=0; j < tissue[i].length; j++)
            {
                double random = r.nextDouble();
    
                if( random <= chanceO )
                    tissue[i][j] = 'O';
                else if( random <= chanceO + chanceX )
                    tissue[i][j] = 'X';
            }
    }