Java 10 * 10网格

时间:2019-02-23 19:25:09

标签: java

在此先发帖,在此先感谢您的帮助。 我已经用Java制作了一个10 * 10的网格,并尝试使行号出现在网格的左侧,在尝试了不同的打印格式和选项后,我现在在这里寻求一些帮助。任何指针将不胜感激。

public class ArrayTest {

public final static int SIZE =10;
final static char[][] GRID = new char[SIZE][SIZE];

public static void main(String[] args){

    setGrid();       
    printGrid();
    }

public static void setGrid(){
    for( int row = 0; row< SIZE;row++){
        for(int column = 0; column<SIZE; column++){
            GRID[row][column]= ' ';
        }
    }
}

public static void printGrid(){
    System.out.println("   10 x 10 Grid");
    System.out.println("    0   1   2   3   4   5   6   7   8   9");
    System.out.println("  +---+---+---+---+---+---+---+---+---+---+");


    for(int row = 0; row< SIZE; row++){
        for(int column = 0; column<SIZE; column++){
               System.out.print("  |" + GRID[row][column] + "");
        }


                System.out.println("  | " + row );
                System.out.println("  +---+---+---+---+---+---+---+---+---+---+");

    }
}

}

2 个答案:

答案 0 :(得分:1)

for(int row = 0; row< SIZE; row++){
   System.out.print( " " + row + " | ");
   for ( int column = 0; column<SIZE; column++){
     ... <print column values for this row here>
   }
   System.out.println("");
 }

当您在顶部打印出列号时,请不要忘记添加额外的空格,以解决行号指示器所用的空间。

答案 1 :(得分:0)

您的算法是一个很好的起点。无论如何,我想扩大您对扩展用例和现有(可靠且可自定义)基于文本的表格格式库的使用的认识。

使用此扩展解决方案并进行优化

这取决于您的方法。我添加了以下功能:

  • 可自定义的网格大小(以 width height 作为参数)
  • 实用程序功能 padRight padLeft repeatChar

您可以进一步优化它,例如:

    标题/数据的
  • 对齐
  • 最大单元空间需求的计算(网格数据的最大长度)

源(使用最少的Java 8流)

在下面或在线查找资源,您可以在test & download on Ideone上找到

import java.util.stream.Collectors;
import java.util.stream.IntStream;

class GridDataToTextTable {

    public static final int WIDTH = 10;
    public static final int HEIGHT = 10;

    public static void main(String[] args) {
        System.out.printf("%d x %d Grid%n", WIDTH, HEIGHT);

        String[][] generateGrid = generateGrid(WIDTH, HEIGHT);

        printGrid(WIDTH, HEIGHT, generateGrid);
    }

    public static String[][] generateGrid(int width, int height) {
        String[][] gridData = new String[height][width];
        for (int row = 0; row < height; row++) {
            for (int column = 0; column < width; column++) {
                gridData[row][column] = " ";
            }
        }
        return gridData;
    }

    public static String padRight(String s, int n) {
        return String.format("%-" + n + "s", s);
    }

    public static String padLeft(String s, int n) {
        return String.format("%" + n + "s", s);
    }

    public static String repeatChar(char c, int n) {
        return IntStream.range(0, n).mapToObj(i -> String.valueOf(c)).collect(Collectors.joining(""));
    }

    public static void printGrid(int width, int height, String[][] gridData) {
        int lengthOfMaxRowNum = String.valueOf(height - 1).length();
        // TODO: can be calculated as max length over Strings in gridData
        int maxCellWidth = 4;

        System.out.print(padRight(" ", lengthOfMaxRowNum));
        for (int column = 0; column < width; column++) {
            System.out.print(padLeft(String.valueOf(column), maxCellWidth + 1));
        }
        System.out.println();
        printHorizontalLine(width, lengthOfMaxRowNum, maxCellWidth);
        System.out.println();

        for (int row = 0; row < height; row++) {
            // TODO: alignment of headers (col-numbers) could be customizable
            System.out.print(padLeft(String.valueOf(row), lengthOfMaxRowNum));
            for (int column = 0; column < width; column++) {
                // TODO: alignment of cell-data could be customizable
                System.out.print("|" + padLeft(gridData[row][column], maxCellWidth));
            }
            System.out.println("|");
            printHorizontalLine(width, lengthOfMaxRowNum, maxCellWidth);
            System.out.println();
        }
    }

    private static void printHorizontalLine(int width, int lengthOfMaxRowNum, int maxCellWidth) {
        String line = repeatChar('-', maxCellWidth);
        System.out.print(padLeft(" ", lengthOfMaxRowNum));
        for (int column = 0; column < width; column++) {
            System.out.printf("+" + line);
        }
        System.out.printf("+");
    }    

}

使用文本格式库

要以表格或网格格式打印基于文本的数据,请参见this answer

也许您可以使用以下库之一: