Java 如何打印数组表?
我来这里是想看看是否有人能帮忙解决这个问题 我正试着打印一张类似这样的表格Java 如何打印数组表?,java,arrays,Java,Arrays,我来这里是想看看是否有人能帮忙解决这个问题 我正试着打印一张类似这样的表格 Month #1 Month #2 Person 1 $1293 $128 Person 2 $122 $1233 我已经完成了获取数字的所有其他步骤,等等,我只是停留在获取正确输出作为表的最后一步 int[][] table = new int[people][month]; // Load the table with
Month #1 Month #2
Person 1 $1293 $128
Person 2 $122 $1233
我已经完成了获取数字的所有其他步骤,等等,我只是停留在获取正确输出作为表的最后一步
int[][] table = new int[people][month];
// Load the table with values
for (int i=0; i < table.length; i++){
for (int j=0; j < table[i].length; j++){
table[i][j] = r.nextInt(20000-1000) + 1000;
}
}
// Print the table
System.out.println("\n\nSummer Internship Salary Information:");
for (int i=0; i < table.length; i++) {
for (int j=0; j < table[i].length; j++)
System.out.print ("Person #" + (i+1) + "$" + table[i][j] + "\t");
System.out.println();
}
(请注意,数字不是正确的数字,只是一个示例)
如何使其具有如下输出:
Month#1 Month#2
Person#1 $12312 $12312
Person#2 $12312 $12312
在本练习中,我不允许使用call方法或JCF。假设所有人的月数相同:
System.out.println("\n\nSummer Internship Salary Information:");
for (int j=0; j < table[0].length; j++) {
System.out.print("\tMonth #" + (j+1));
}
for (int i=0; i < table.length; i++) {
System.out.print("\nPerson #" + (i+1));
for (int j=0; j < table[i].length; j++) {
System.out.print("\t$" + table[i][j]);
}
}
System.out.println();
System.out.println(“\n\n用户实习工资信息:”);
对于(int j=0;j<表[0]。长度;j++){
System.out.print(“\tMonth#”+(j+1));
}
对于(int i=0;i
请注意,Person#将从内部循环中取出,列标题将首先打印出来
还要注意,如果任何数字太宽(比制表符宽),它将破坏布局。要解决这个问题,您必须更聪明(首先找到每列的最大宽度或截断值)
(经过编辑,可以将制表符和换行符放在更好的位置;字符串更少,没有尾随制表符)假设所有人的月数相同:
System.out.println("\n\nSummer Internship Salary Information:");
for (int j=0; j < table[0].length; j++) {
System.out.print("\tMonth #" + (j+1));
}
for (int i=0; i < table.length; i++) {
System.out.print("\nPerson #" + (i+1));
for (int j=0; j < table[i].length; j++) {
System.out.print("\t$" + table[i][j]);
}
}
System.out.println();
System.out.println(“\n\n用户实习工资信息:”);
对于(int j=0;j<表[0]。长度;j++){
System.out.print(“\tMonth#”+(j+1));
}
对于(int i=0;i
请注意,Person#将从内部循环中取出,列标题将首先打印出来
还要注意,如果任何数字太宽(比制表符宽),它将破坏布局。要解决这个问题,您必须更聪明(首先找到每列的最大宽度或截断值)
(编辑以将制表符和换行符放在更好的位置;更少的字符串和没有尾随制表符)由于列的可变性质,我还将计算每列的“所需宽度”。这将用于“填充”较短的列,以确保列对齐 这将允许在不需要任何额外补偿的情况下增加人员数量和工资规模
public class SalaryColumns {
public static void main(String[] args) {
int people = 20;
int month = 12;
String monthLabel = "Month #";
String personLabel = "Person #";
Random r = new Random(System.currentTimeMillis());
int[][] table = new int[people][month];
int[] columWidths = new int[month + 1];
columWidths[0] = personLabel.length() + Integer.toString(people).length() + 1;
// Load the table with values
for (int i = 0; i < table.length; i++) {
for (int j = 0; j < table[i].length; j++) {
table[i][j] = r.nextInt(20000 - 1000) + 1000;
columWidths[j + 1] = Math.max(
columWidths[j + 1],
Math.max(
monthLabel.length() + Integer.toString(month).length() + 1,
Integer.toString(table[i][j]).length() + 2));
}
}
// Print the table
System.out.println("\n\nSummer Internship Salary Information:");
System.out.print(pad("", columWidths[0]));
for (int i = 0; i < month; i++) {
String value = monthLabel + String.format("%d", i);
value += pad(value, columWidths[i + 1]);
System.out.print(value);
}
System.out.println();
for (int i = 0; i < table.length; i++) {
String value = personLabel + String.format("%d", i);
value += pad(value, columWidths[0]);
System.out.print(value);
for (int j = 0; j < table[i].length; j++) {
value = String.format("$%d", table[i][j]);
value += pad(value, columWidths[j + 1]);
System.out.print(value);
}
System.out.println();
}
}
public static String pad(String value, int length) {
StringBuilder sb = new StringBuilder(length);
while ((value.length() + sb.length()) < length) {
sb.append(" ");
}
return sb.toString();
}
}
由于列的可变性质,我还将计算每列的“所需宽度”。这将用于“填充”较短的列,以确保列对齐 这将允许在不需要任何额外补偿的情况下增加人员数量和工资规模
public class SalaryColumns {
public static void main(String[] args) {
int people = 20;
int month = 12;
String monthLabel = "Month #";
String personLabel = "Person #";
Random r = new Random(System.currentTimeMillis());
int[][] table = new int[people][month];
int[] columWidths = new int[month + 1];
columWidths[0] = personLabel.length() + Integer.toString(people).length() + 1;
// Load the table with values
for (int i = 0; i < table.length; i++) {
for (int j = 0; j < table[i].length; j++) {
table[i][j] = r.nextInt(20000 - 1000) + 1000;
columWidths[j + 1] = Math.max(
columWidths[j + 1],
Math.max(
monthLabel.length() + Integer.toString(month).length() + 1,
Integer.toString(table[i][j]).length() + 2));
}
}
// Print the table
System.out.println("\n\nSummer Internship Salary Information:");
System.out.print(pad("", columWidths[0]));
for (int i = 0; i < month; i++) {
String value = monthLabel + String.format("%d", i);
value += pad(value, columWidths[i + 1]);
System.out.print(value);
}
System.out.println();
for (int i = 0; i < table.length; i++) {
String value = personLabel + String.format("%d", i);
value += pad(value, columWidths[0]);
System.out.print(value);
for (int j = 0; j < table[i].length; j++) {
value = String.format("$%d", table[i][j]);
value += pad(value, columWidths[j + 1]);
System.out.print(value);
}
System.out.println();
}
}
public static String pad(String value, int length) {
StringBuilder sb = new StringBuilder(length);
while ((value.length() + sb.length()) < length) {
sb.append(" ");
}
return sb.toString();
}
}
我知道这篇文章很老了,但因为我经常陷入这个问题,可能是因为在互联网上很多人都在看,所以我最终想发表我的答案 我编写了一个非常简单的类,它使用
String.format
方法格式化对象的通用表:
public class TableFormatter {
private int columns;
private List<String> cells = new LinkedList<>();
private int minSpacesBetweenCells = 4;
private boolean alignLeft = true;
private int maxLength = 0;
public TableFormatter(int columns) {
this.columns = columns;
}
public TableFormatter insert(Object... cells) {
for (Object content : cells) {
String cell = content.toString();
maxLength = Math.max(maxLength, cell.length());
this.cells.add(cell);
}
return this;
}
public TableFormatter setMinSpacesBetweenCells(int minSpacesBetweenCells) {
this.minSpacesBetweenCells = minSpacesBetweenCells;
return this;
}
public TableFormatter alignCellsToRight() {
this.alignLeft = false;
return this;
}
public TableFormatter alignCellsToLeft() {
this.alignLeft = true;
return this;
}
@Override
public String toString() {
String format = "%";
if (alignLeft)
format += "-";
format += maxLength + "s";
String spaces = new String(new char[minSpacesBetweenCells]).replace("\0", " ");
StringBuilder sb = new StringBuilder();
int row = 0;
int currentColumn = 0;
for (String cell : cells) {
if (currentColumn == 0) {
if (row > 0)
sb.append("\n");
} else {
sb.append(spaces);
}
sb.append(String.format(format, cell));
currentColumn = (currentColumn + 1) % columns;
if (currentColumn == 0)
row++;
}
return sb.toString();
}
public static void main(String[] args) {
TableFormatter tableFormatter = new TableFormatter(3);
tableFormatter.insert("column1", "column2", "column3");
tableFormatter.insert("e11", 12, "e13");
tableFormatter.insert("e21", "e22", 23);
tableFormatter.insert(3.1d, "e32", "e33");
tableFormatter.insert("e41", "e42", true);
System.out.println(tableFormatter);
}
}
我希望这篇文章对下一位读者有用。我知道这篇文章很老了,但是,因为我经常陷入这个问题,可能是因为在互联网上很多人都在看,我最终想发表我的答案 我编写了一个非常简单的类,它使用
String.format
方法格式化对象的通用表:
public class TableFormatter {
private int columns;
private List<String> cells = new LinkedList<>();
private int minSpacesBetweenCells = 4;
private boolean alignLeft = true;
private int maxLength = 0;
public TableFormatter(int columns) {
this.columns = columns;
}
public TableFormatter insert(Object... cells) {
for (Object content : cells) {
String cell = content.toString();
maxLength = Math.max(maxLength, cell.length());
this.cells.add(cell);
}
return this;
}
public TableFormatter setMinSpacesBetweenCells(int minSpacesBetweenCells) {
this.minSpacesBetweenCells = minSpacesBetweenCells;
return this;
}
public TableFormatter alignCellsToRight() {
this.alignLeft = false;
return this;
}
public TableFormatter alignCellsToLeft() {
this.alignLeft = true;
return this;
}
@Override
public String toString() {
String format = "%";
if (alignLeft)
format += "-";
format += maxLength + "s";
String spaces = new String(new char[minSpacesBetweenCells]).replace("\0", " ");
StringBuilder sb = new StringBuilder();
int row = 0;
int currentColumn = 0;
for (String cell : cells) {
if (currentColumn == 0) {
if (row > 0)
sb.append("\n");
} else {
sb.append(spaces);
}
sb.append(String.format(format, cell));
currentColumn = (currentColumn + 1) % columns;
if (currentColumn == 0)
row++;
}
return sb.toString();
}
public static void main(String[] args) {
TableFormatter tableFormatter = new TableFormatter(3);
tableFormatter.insert("column1", "column2", "column3");
tableFormatter.insert("e11", 12, "e13");
tableFormatter.insert("e21", "e22", 23);
tableFormatter.insert(3.1d, "e32", "e33");
tableFormatter.insert("e41", "e42", true);
System.out.println(tableFormatter);
}
}
我希望这对本帖的下一位访问者有用。请注意:以后,在一个问题标签中指定编程语言。这个问题中没有特别提到Java,所以了解Java的人可以跳过它!非常感谢。请注意:将来,在一个问号中指定编程语言。这个问题中没有特别提到Java,所以了解Java的人可以跳过它!非常感谢。哦我现在明白了。非常感谢,没问题。如果这回答了您的问题,您应该将其标记为答案,以便将其从“未回答”选项卡中删除。不会说谎;这也给了我分数!哦我现在明白了。非常感谢,没问题。如果这回答了您的问题,您应该将其标记为答案,以便将其从“未回答”选项卡中删除。不会说谎;这也给了我分数!