我们可以使用 printf 在 java 中使用空格来填充输出以适应“固定宽度列”。示例:
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
最佳答案
看起来之前已经尝试过 Change default padding character in java-printf 。建议使用一种解决方法,但它可能适合您的场景,尽管您也可以修改 java 源代码,但我认为每个人都会强烈建议不要这样做。
关于带有重复句点的java printf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17778745/