How to make a 20 rows x 50 columns grid in java

笑着哭i 提交于 2019-12-12 19:51:22

问题


I'm trying to make a grid made out of "." for a MineSweeper/Capture The Flag type game, but I've been having trouble. I'm trying to do a \n every 50 "." so it can start printing another column but my code prints a single dot every row. This is how the grid is supposed to look (ignore the % and the since that's another part of the project, pretend it is alll "."): https://imgur.com/a/3zWKyb8

This is my code:

 String grid = ".";
    int rows = 20;
    int columns = 50;
    int count = 0;

    while(count <= 1000)
    {
        count++;

        for(int c = 1; c <= columns; display(grid))
        {
            String nwln = "\n";
            display(nwln);
            c = 0;
        }
    }

My display method code per requested:

public static String display(String disp)
{
    System.out.print(disp);
    return(disp);
}

回答1:


First let's see a simple code to print the 20 X 50 grid:

public static void main(String[] args) {
    final String point = ".";
    final int rows = 20;
    final int columns = 50;

    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < columns; j++) {
            System.out.print(point);
        }
        System.out.println();
    }
}

From there you can implement your flags and bombs in between the points.



来源:https://stackoverflow.com/questions/54523029/how-to-make-a-20-rows-x-50-columns-grid-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!