如何将数字放入 space 中(如果存在),否则放入 space 中?

How to put a number in a space if it exists, but a space otherwise?

如何打印到一行," 1" 如果是一个,但是 " 10" 如果是十,或者 "100" 或者是一百?

本质上不管怎么说都是三个字符space,但如果数字足够大就是一个数字;否则,它是 space.

检查 printf() (docs here) 方法。

示例:

System.out.printf("%1d%n", 1);
System.out.printf("%2d%n", 1);
System.out.printf("%3d%n", 1);

输出:

1
 1
  1

如果你想打印前导零,你可以这样做:

System.out.printf("%03d%n", 1);

输出:

001