我想组织输出,所以我想做这样的事情
1 2 3 4 5
........
91 92 93 94 95
........
101 102 103 104 105
etc....
所以我试图弄清楚如何将它们排列成每个数字之间正确的间距。
我的第一次尝试是像这样手动完成所有事情
System.out.print(" " + counter);
使用循环
最佳答案
阅读Formatting Numeric Print Output在文档中。
您可以使用System.out.format
使用%<width>d
在哪里替换 <width>
根据您需要的宽度(确保所有内容都适合!):
System.out.format("%4d", number); // align to width of 4
要添加换行符,请使用 %n
:
System.out.format("%4d%n", 1);
System.out.format("%4d%n", 12);
System.out.format("%4d%n", 123);
关于java - 如何打印出数字之间的特定间距长度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32446192/