简体   繁体   English

我如何在随机点 JAVA 中填充具有某些值的数组

[英]how do i populating an array with certain values in random spots JAVA

public static void main(String[] args) {
        int castleTiles[][] = new int[8][8];

        for(int i = 0; i < castleTiles.length; i++) {
            for(int j = 0; j < castleTiles[i].length; j++) {
                castleTiles[i][j] = 0;
            }
        }




    for(int i = 0; i < castleTiles.length; i++) {
        for(int j = 0; j < castleTiles[i].length; j++) {
            System.out.printf("%3d", castleTiles[i][j]);
        }
        System.out.println();
    }


}

I need to insert the number 1 5 times in 5 random different spots in the array and the number 2 once at a random spot in the array我需要在数组中的 5 个随机不同位置插入数字 1 5 次,在数组中的随机位置插入数字 2 一次

在此处输入图片说明

 import java.util.Random;
    
    public class HelloWorld{
    
         public static void main(String[] args) {
            int castleTiles[][] = new int[8][8];
    
            for(int i = 0; i < castleTiles.length; i++) {
                for(int j = 0; j < castleTiles[i].length; j++) {
                    castleTiles[i][j] = 0;
                }
            }
    
    
    
    Random rn = new Random();
    
    for(int i =0; i < 5; i++)
    {
        int x = rn.nextInt(7) + 0;
        int y = rn.nextInt(7) + 0;
        castleTiles[x][y] = 1;
    }
    
        int x = rn.nextInt(7) + 0;
        int y = rn.nextInt(7) + 0;
        castleTiles[x][y] = 2;
    
    
    
        for(int i = 0; i < castleTiles.length; i++) {
            for(int j = 0; j < castleTiles[i].length; j++) {
                System.out.printf("%3d", castleTiles[i][j]);
            }
            System.out.println();
        }
    
    
    }
    }

First, there's no need to set 0 to castleTiles array upon its creation, int arrays are filled with 0 by default.首先,不需要在创建castleTiles数组时将其设置为 0,默认情况下int数组填充为 0。

Next, it may be better to create an array/list of the spot values to be added to the tile (eg {1, 1, 1, 1, 1, 2} ).接下来,最好创建一个要添加到图块中的点值的数组/列表(例如{1, 1, 1, 1, 1, 2} )。

And the last, when populating the array, we should check if we put the spot into an empty cell:最后,在填充数组时,我们应该检查是否将点放入空单元格中:

int castleTiles[][] = new int[8][8];

int[] spots = {1, 1, 1, 1, 1, 2};

Random random = new Random();
for (int s : spots) {
    while (true) {
        int r = random.nextInt(64);
        int x = r / 8;
        int y = r % 8;
        if (castleTiles[x][y] == 0) {
            castleTiles[x][y] = s;
            break;
        }
    }
}

After displaying .显示后. instead of 0 the casteTiles may look as follows:而不是 0, casteTiles可能如下所示:

for(int i = 0; i < castleTiles.length; i++) {
    for(int j = 0; j < castleTiles[i].length; j++) {
        System.out.print("  " + (castleTiles[i][j] == 0 ? "." : Integer.toString(castleTiles[i][j])));
    }
    System.out.println();
}
  .  .  .  .  .  .  .  1
  .  .  .  .  .  2  .  .
  .  .  1  .  .  .  .  .
  .  .  .  .  .  .  .  .
  1  .  1  .  .  .  .  .
  .  .  .  .  .  .  .  .
  .  1  .  .  .  .  .  .
  .  .  .  .  .  .  .  .

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

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