2

我们可以使用空格来填充输出以适应 java 中使用 printf 的“固定宽度列”。例子:

System.out.printf("%-30s - %s%n", "hello hello", "goodbye goodbye");
System.out.printf("%-30s - %s%n", "lots of cats",
        "large amounts of dogs");
System.out.printf("%-30s - %s%n", "a crowd of teachers", "no students");
System.out.printf("%-30s - %s%n", "three desks", "twenty boxes");

给出输出

hello hello                    - goodbye goodbye
lots of cats                   - large amounts of dogs
a crowd of teachers            - no students
three desks                    - twenty boxes

我想要实现的是:

hello hello. . . . . . . . . . . goodbye goodbye
lots of cats . . . . . . . . . . large amounts of dogs
a crowd of teachers. . . . . . . no students
three desks. . . . . . . . . . . twenty boxes

或者

hello hello..................... goodbye goodbye
lots of cats.................... large amounts of dogs
a crowd of teachers............. no students
three desks..................... twenty boxes
4

1 回答 1

2

看起来在更改 java-printf 中的默认填充字符之前已尝试过此操作。建议了一种解决方法,但它可能适合您的场景,尽管您也可以修改 java 源代码,但我认为每个人都会强烈建议不要这样做。

于 2013-07-22T01:42:31.463 回答