繁体   English   中英

如何在 Java 中自动填充数组。 每个字符的特定数量

[英]How to populate automatically an array in Java. With specific number of each character

我想用一定数量的每个字符自动填充这个数组。

例如 30 个带有@的索引,20 个带有%的索引。 “空” arrays 将是. .

public class Main {

    public static void main(String[] args) {
        char grid[][] = new char[20][40];
        Scanner move = new Scanner(System.in);
        System.out.println("Let's start playing:");
        System.out.println("---------------------------------------------------------------");
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[i].length; j++) {
                grid [i][j] = '.';
                System.out.print(grid[i][j]);
            }
            System.out.println();
        }
    }
}

我假设你想在你的数组中随机分布字符。 我将继续假设值 30 和 20 是指导方针,而不是绝对的。 由于您有一个可以存储 800 个字符的 20x40 数组,其中应该有大约 750 个点,另外 50 个是其他字符,那么分布将是'.' 93.75%。 , '%' 2.5% 和'@' 3.75%。 使用java.util.random这样的东西可能是一个起点:

import java.util.Random;
....


public static void main(String[] args) {
    char grid[][] = new char[20][40];
    Scanner move = new Scanner(System.in);
    Random rand = new Random();
    System.out.println("Let's start playing:");
    System.out.println("---------------------------------------------------------------");
    for (int i = 0; i < grid.length; i++) {
        for (int j = 0; j < grid[i].length; j++) {
            double r = rand.nextDouble();
            grid [i][j] = r < 0.025 ? '%' : r < 0.0625 ? '@' : '.';
            System.out.print(grid[i][j]);
        }
        System.out.println();
    }
}

如果我理解正确,您想为要添加到矩阵的每个字符设置数量。 作为初学者,我可以用初学者工具回答,这是我的尝试:

public static void main(String[] args)
{
    char grid[][] = new char[20][40];
    Scanner move = new Scanner(System.in);
    System.out.println("Let's start playing:");
    System.out.println("---------------------------------------------------------------");
    for (int v = 0; v < grid.length; v++) {
        for (int w = 0; w < grid[v].length; w++) {
            grid [v][w] = '.';
            System.out.print(grid[v][w]);
        }
        System.out.println();
    }
    System.out.println("Enter amount of @ to added. enter up to-"+grid.length*grid[0].length);
    int at = move.nextInt();
    int count = 0;
    int i = 0;
    int j = 0;

    while(count <= at && i < grid.length)
    {
        while(count <= at){
            grid[i][j++] = '@';
            count++;
            if(j > grid[i].length-1){
                j = 0;
                break;
            }
        }
        i++;
    }

    System.out.println("Enter amount of % to added. enter up to-"+(grid.length*grid[0].length-at));
    int modules = move.nextInt();
    count = 0;
    i--; // start from same line that ended with
    while(count <= modules && i < grid.length)
    {
        while(count <= modules){
            grid[i][j++] = '%';
            count++;
            if(j > grid[i].length-1){
                j = 0;
                break;
            }
        }
        i++;
    }
    // loops to print result:
    for (int k = 0; k < grid.length; k++) {
        for (int l = 0; l < grid[k].length; l++) {
            System.out.print(grid[k][l]);
        }
        System.out.println();
    }
}   

暂无
暂无

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

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