我在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相同?
答案 0 :(得分: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());
答案 1 :(得分: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