Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:如何使用特定数量的元素创建20x20 2D数组_Java - Fatal编程技术网

Java:如何使用特定数量的元素创建20x20 2D数组

Java:如何使用特定数量的元素创建20x20 2D数组,java,Java,我在Java上尝试创建一个20x20字符的2D数组,其中包含特定数量的元素。例如 A x 20 B X 10 C是其余的 其中20x20网格周围随机放置20个As,20x20网格周围随机放置10个Bs,并用C填充所有空白 我试图用Math.random()实现这一点,但似乎做不到 package quest; import java.util.*; public class Quest { public static void main(String[] args) {

我在Java上尝试创建一个20x20字符的2D数组,其中包含特定数量的元素。例如 A x 20 B X 10 C是其余的

其中20x20网格周围随机放置20个As,20x20网格周围随机放置10个Bs,并用C填充所有空白

我试图用Math.random()实现这一点,但似乎做不到

package quest;
import java.util.*;
public class Quest 
{
    public static void main(String[] args) 
    {
    char [][] board = new char [20][20];
    int j = 0;
    for(int x = 0; x < 20; x++)
    {
    for(int y = 0; y < 20; y++)
    {
    for(int a = 0; a < 20; a++)
    {    
    int chances = (int)(Math.random()*20)+1;      
    if (chances == 1)
    {    
    board[x][y] = 'A';
    }
    }    
    for(int b = 0; b < 10; b++)
    {    
    int chances = (int)(Math.random()*20)+1;      
    if (chances == 1)
    {    
    board[x][y] = 'B';
    }
    if(board[x][y] == 0 )
    {
    board[x][y] = 'C';
    }     
    }        
    System.out.print(board[x][y]+"  ");
    }
    System.out.println();
    }

}

}
packagequest;
导入java.util.*;
公开课探索
{
公共静态void main(字符串[]args)
{
字符[][]板=新字符[20][20];
int j=0;
对于(int x=0;x<20;x++)
{
对于(int y=0;y<20;y++)
{
对于(int a=0;a<20;a++)
{    
int机会=(int)(Math.random()*20)+1;
如果(机会==1)
{    
董事会[x][y]=“A”;
}
}    
对于(int b=0;b<10;b++)
{    
int机会=(int)(Math.random()*20)+1;
如果(机会==1)
{    
董事会[x][y]=“B”;
}
如果(电路板[x][y]==0)
{
板[x][y]=“C”;
}     
}        
系统输出打印(单板[x][y]+”);
}
System.out.println();
}
}
}

您只需生成一个随机数,如果它小于0.5,则分配并
a
,否则分配a
B
。然后,您将生成另一个随机数来确定字母的位置。如果该位置已被占用,则生成另一个位置,依此类推

当你用完一封信时,比如说,你已经分配了20个
a
,你只需随机生成位置并将
B
分配给该位置


完成后,您只需遍历数组,如果发现任何空白,则分配
C

此解决方案生成一个400(
20x20
)唯一随机数序列,从0到399,覆盖
20x20
网格中的每个插槽。然后随机填充20个As、10个Bs和其余的Cs

使用
Math.random
的问题在于,您可能会多次生成相同的随机数。更好的方法是对覆盖2D数组中每个插槽的数字序列使用
Collections.shuffle()

int WIDTH = 20;
int HEIGHT = 20;
char[][] array = new char[WIDTH][HEIGHT]; 

// generate a random sequence of 400 (= 20 x 20) unique numbers
ArrayList<Integer> numbers = new ArrayList<Integer>();

for (int i = 0; i < WIDTH*HEIGHT; i++) {
    numbers.add(i);
}
Collections.shuffle(numbers);

// add 20 randomly placed As
for (int i=0; i < 20; ++i) {
    int r = numbers.get(i) / WIDTH;
    int c = numbers.get(i) % HEIGHT;

    array[r][c] = 'A';
}

// add 10 randomly placed Bs
for (int i=20; i < 30; ++i) {
    int r = numbers.get(i) / WIDTH;
    int c = numbers.get(i) % HEIGHT;

    array[r][c] = 'B';
}

// fill the rest with Cs
for (int i=30; i < WIDTH*HEIGHT; ++i) {
    int r = numbers.get(i) / WIDTH;
    int c = numbers.get(i) % HEIGHT;

    array[r][c] = 'C';
}
int-WIDTH=20;
整数高度=20;
字符[][]数组=新字符[宽度][高度];
//生成400(=20 x 20)个唯一数字的随机序列
ArrayList编号=新的ArrayList();
对于(int i=0;i
这里我向您展示如何添加为。对于Bs,你也这样做。对于Cs,您只需通过您的电路板,将Cs插入空单元

 char[][] board = new Char[20][20];
 //Place As
 for (int i = 1; i <= 20; i++) { //20 As
     int randX = (int) (Math.random() * 20); //random number between 0-19
     int randY = (int) (Math.random() * 20);
     while (board[randX][randY] != null) { //find a free cell
         randX = (int) (Math.random() * 20);
         randY = (int) (Math.random() * 20);
      }
      board[randX][randY] = 'A'; //insert A
  }
char[]board=新字符[20][20];
//作为

对于(int i=1;i您可以将20x20 2D数组视为一个400元素的1D数组。使用一点数学知识,您可以获得值应该放置的坐标。之后,您可以为a和B字符的放置位置生成随机数。您必须进行检查,以确保不会覆盖已设置的a or B字符

    final char[][] matrix = new char[20][20];
    for (int i = 0; i < 20; i++) {
        for (int j = 0; j < 20; j++) {
            matrix[i][j] = 'c';
        }
    }
    final Random random = new Random();
    for (int i = 0; i < 20; i++) {
        while(true) {
            int position = random.nextInt(400);
            if (matrix[position / 20][position % 20] == 'c') {
                matrix[position / 20][position % 20] = 'a';
                break;
            }
        }
    }
    for (int i = 0; i < 10; i++) {
        while(true) {
            int position = random.nextInt(400);
            if (matrix[position / 20][position % 20] == 'c') {
                matrix[position / 20][position % 20] = 'b';
                break;
            }
        }
    }
final char[][]矩阵=新char[20][20];
对于(int i=0;i<20;i++){
对于(int j=0;j<20;j++){
矩阵[i][j]=‘c’;
}
}
最终随机数=新随机数();
对于(int i=0;i<20;i++){
while(true){
int位置=随机的nextInt(400);
如果(矩阵[position/20][position%20]=='c'){
矩阵[位置/20][位置%20]=“a”;
打破
}
}
}
对于(int i=0;i<10;i++){
while(true){
int位置=随机的nextInt(400);
如果(矩阵[position/20][position%20]=='c'){
矩阵[位置/20][位置%20]=“b”;
打破
}
}
}

谢谢,这很简单,这就是我一直在寻找的答案!