Java程序使用Formatter在输出中显示带有列的表
要显示带有列作为输出的表,请使用Formatter类。为了使用Formatter类,请导入以下包-
import java.util.Formatter;
考虑一个包含一些元素的数组。这是数组,我们将以表格形式将其显示为输出-
double arr[] = { 1.7, 2.5, 3.1, 4.5, 5.7, 6.9, 7.7, 8.9, 9.1 };显示双精度数组值时,使用%f设置空格-
for (double d : arr) {
f.format("%14.2f %14.2f %15.2f\n", d, Math.ceil(d), Math.floor(d));
}以下是一个例子-
示例
import java.util.Formatter;
public class Demo {
public static void main(String[] argv) throws Exception {
double arr[] = { 1.7, 2.5, 3.1, 4.5, 5.7, 6.9, 7.7, 8.9, 9.1 };
Formatter f = new Formatter();
f.format("%15s %15s %15s\n", "Points1", "Points2", "Points3");
System.out.println("The point list...\n");
for (double d : arr) {
f.format("%14.2f %14.2f %15.2f\n", d, Math.ceil(d), Math.floor(d));
}
System.out.println(f);
}
}输出结果
The point list... Points1 Points2 Points3 1.70 2.00 1.00 2.50 3.00 2.00 3.10 4.00 3.00 4.50 5.00 4.00 5.70 6.00 5.00 6.90 7.00 6.00 7.70 8.00 7.00 8.90 9.00 8.00 9.10 10.00 9.00
热门推荐
1 哈尼族祝福语简短
6 七十年祝福语简短
10 新娘生孩子祝福语简短
11 下午微信祝福语简短
12 简单成语祝福语大全简短
13 手写妈妈生日祝福语简短
14 经典座右铭简短祝福语英文
15 结婚新娘妈妈祝福语简短
16 冬至宾馆祝福语简短独特
17 带猫的祝福语简短
18 乔迁祝福语简短精辟句子