2

I notice that println() does this, but I cannot get it to work in printf().

In the following snippet, the first two println's give RED and BLUE, then the printf crashes.

class Jim {
public enum Colours  {BLUE, RED}
static Colours xx = Colours.RED;
public static void main(String[] args)  {
    Colours fred = Colours.BLUE;
    System.out.println(xx);
    System.out.println(fred);
    System.out.printf("%d, %d\n", fred, xx);
}
4

1 回答 1

8

%d格式说明符与类型不兼容,enum除非针对ordinal值使用。要调用该enum's toString方法(使用该name字段),您可以%s使用

System.out.printf("%s, %s%n", fred, xx);

阅读:枚举

于 2013-09-19T10:34:50.210 回答