-
Java中实现格式化输出的几种方式:
1、System.out.printf();
-
类似于c语言的printf方法。如:
int x = 55;
System.out.format(“x = %5x”, x);
输出结果为:x = 37
2、System.out.format()
-
int x = 55;
System.out.printf(“x = %5c”, x);
输出结果为:x = 7
3、String.format()
-
String提供的静态方法。
int x = 55;
System.out.println(String.format(“x = %5x”, x));
输出结果为:x = 37
4、Formatter
-
int x = 55;
Formatter format = new Formatter(System.out);
format.format(“x = %5x”, x);
输出结果为:x = 37
Java中格式化输出的几种方法
最新推荐文章于 2024-05-12 17:21:10 发布