方法一: for 循环判断位数; 补零个数是,进行总长度减去数字长度。
方法二: 善用String.format 方法 如:
public static void main(String[] args) { // 0 代表前面补充0 // 4 代表长度为4 // d 代表参数为正数型 int[] nums = new int[] { 1, 10, 100, 1000 }; for (int d : nums) { System.out.PRintln(String.format("%04d", d)); } }新闻热点
疑难解答