0

我用java“CircleDemo”编写了一个程序,不可避免地会这样做:

System.out.printf("The circle's area \t   is \t %.2f \n", circle.getArea());
System.out.printf("The circle's diameter \t   is \t %.2f \n", circle.getDiameter());
System.out.printf("The circle's circumference is \t %.2f \n", circle.getCircumference());

在 cmd.exe 中显示如下:

The circle's area          is    3.14 
The circle's diameter      is    2.00
The circle's circumference is    6.28

漂亮,干净的格式。

但是,Notepad++ 的控制台 (nppexec) 打印相同的程序,如下所示:

The circle's area     is    3.14 
The circle's diameter      is    2.00
The circle's circumference is    6.28

你可以看到格式是如何不同的。现在,我已经玩了足够长的时间来找到导致这种情况的原因是“\t”在 cmd.exe 与 Notepad++ 的 nppexec 中打印选项卡的方式不同。

我如何编辑 nppexec 的“\t”格式来打印“\t”,就像 cmd.exe 一样?

4

2 回答 2

2

避免使用\t获取数组格式。我可能会简单地使用

System.out.printf("The circle's area          is %.2f %n", circle.getArea() );
System.out.printf("The circle's diameter      is %.2f %n", circle.getDiameter());
System.out.printf("The circle's circumference is %.2f %n", circle.getCircumference());

如果需要更多动态解决方案,您可以使用

System.out.printf("The circle's %-13s is %.2f%n", "area", circle.getArea());

或者如果响应不需要总是以The circle's

System.out.printf("%-26s is %.2f%n", "The circle's area",  circle.getArea());

演示

于 2015-07-03T16:14:02.887 回答
1

你可以试试下面的例子。请在宽度前使用“-”以确保左缩进。默认情况下,它们将正确缩进;这可能不适合您的目的。

    System.out.printf("%-30s %-5s $%.2f\n","The circle's area ", "is" ,circle.getArea());
    System.out.printf("%-30s %-5s $%.2f\n","The circle's diameter ", "is" ,circle.getDiameter());
    System.out.printf("%-30s %-5s $%.2f\n","The circle's circumference ", "is" ,circle.getCircumference());

来源:http ://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html#syntax

于 2015-07-03T15:47:05.653 回答