首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Java 10 * 10网格

Java 10 * 10网格
EN

Stack Overflow用户
提问于 2019-02-23 19:25:08
回答 2查看 564关注 0票数 2

在这里第一篇文章,谢谢你的帮助。我在Java中创建了10*10网格,并试图将行号显示在网格的左侧,经过多次尝试不同的打印格式和选项之后,我现在在这里做了一些帮助。任何指示都将受到极大的赞赏。

代码语言:javascript
运行
复制
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("  +---+---+---+---+---+---+---+---+---+---+");

    }
}

}

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-02-23 19:38:58

代码语言:javascript
运行
复制
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
EN

Stack Overflow用户

发布于 2019-02-23 19:42:37

您的算法是一个很好的起点。无论如何,我想拓宽您对、扩展用例和现有(可靠和可定制)基于文本的表格格式库的使用的看法。

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

这是基于你的方法。我添加了以下特性:

  • 可自定义网格尺寸的(宽度、高度作为参数)
  • 实用程序函数:padRight,padLeft,repeatChar

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

  • 头/数据的左或右对齐
  • 计算最大单元空间需要(网格数据的最大长度)

源(最少使用Java 8流)

可以在下面或网上找到可以艾德龙的源。

代码语言:javascript
运行
复制
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("+");
    }    

}

使用文本格式库

若要以表格或网格格式打印基于数据文本的数据,请参见这个答案

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54845307

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档