【问题标题】:Java printf Flags orderJava printf 标志顺序
【发布时间】:2018-07-01 13:34:20
【问题描述】:

在 printf 中使用标志时我必须遵循任何特定顺序吗?

1.System.out.printf("%+04d + %s ", 12, "a");// include sign then pad zeros      
2.System.out.printf("\n%04+d + %s ", 12, "a");// pad zeros then include sign    
3.System.out.printf("\n%+-4d + %s ", 12, "a");// include sign then left justify 
4.System.out.printf("\n%-4+d + %s ", 12, "a");// left justify then include sign 


The answer is: 

+012 + a 
Exception in thread "main" java.util.UnknownFormatConversionException: Conversion = '0'
+12  + a 
Exception in thread "main" java.util.UnknownFormatConversionException: Conversion = '-'     

谢谢。

【问题讨论】:

标签: java printf formatexception


【解决方案1】:

是的,顺序很重要,这就是为什么某些组合会出现异常:

% [flags][width][.precision] conversion-character (square brackets denote optional parameters)

谢谢

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-25
    • 2020-02-18
    相关资源
    最近更新 更多