String result = String.format( "% % % | %-20s| %25s | $%,8.2f | %5d | %-s", thisYear, thisMonth, thisDay,
                    eventName, eventVenue, ticketPrice, ticketsRemaining, eventType);


整数:今年,本月,今天,门票剩余。

字符串:eventName,eventVenue,eventType。

双:ticketPrice。

我已验证所有字段均已正确初始化。此行应以以下格式输出:

2012-01-20 | Penguins-Capitals    |      CONSOL Energy Center | $   92.35 |    15 | NHL
2012-08-05 | Chickenfoot          |    Riverplex Amphitheatre | $   62.50 | 1,273 | Concert


格式化程序不完整,但是尝试运行该格式化程序时会失败,因此其中存在一些语法错误。我对格式化程序不是很好。

最佳答案

您应确保始终在%之后添加类型,在本例中为%d

String result = String.format( "%4d-%02d-%02d | %-20s| %25s | $%.3f | %5d | %s", thisYear, thisMonth, thisDay,
                eventName, eventVenue, ticketPrice, ticketsRemaining, eventType);


要制作,而不是.,请观看此thread

10-07 12:43
查看更多