在这里第一篇文章,谢谢你的帮助。我在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(" +---+---+---+---+---+---+---+---+---+---+");
}
}}
发布于 2019-02-23 19:38:58
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("");
}当您在顶部打印列号时,不要忘记添加额外的空格,以说明行号指示符所用的空间。
发布于 2019-02-23 19:42:37
您的算法是一个很好的起点。无论如何,我想拓宽您对、扩展用例和现有(可靠和可定制)基于文本的表格格式库的使用的看法。
使用此扩展解决方案并优化
这是基于你的方法。我添加了以下特性:
您可以进一步优化这一点,例如:
源(最少使用Java 8流)
可以在下面或网上找到可以艾德龙的源。
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("+");
}
}使用文本格式库
若要以表格或网格格式打印基于数据文本的数据,请参见这个答案。
也许您可以使用以下库之一:
https://stackoverflow.com/questions/54845307
复制相似问题