【发布时间】:2017-09-29 22:01:59
【问题描述】:
我必须左对齐站名并右对齐下面示例中的数字结果以进行分配,即 Vegan Station 需要在宽度为 15 的字段中左对齐,但是当我尝试时它给了我结果的 %2d 错误。如果有人能帮助我理解如何正确格式化它,我将不胜感激。
System.out.printf("\n" + "\n" + "You rated each station as follows");
System.out.printf("\n" + "%-15s, Vegan Station" + "%2d", vegan);
System.out.printf("\n" + "Pasta Station " + "%2d" , pasta);
System.out.printf("\n" + "Waffle Station " + "%2s", waffle + "\n");
这是错误:
java.util.MissingFormatArgumentException: Format specifier '%2d'
at java.util.Formatter.format(Formatter.java:2519)
at java.io.PrintStream.format(PrintStream.java:970)
at java.io.PrintStream.printf(PrintStream.java:871)
at Survey.main(Survey.java:127)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
【问题讨论】:
-
能否包括素食、意大利面和华夫饼的种类?