Java 如何在三元运算符中使用枚举检查两个条件?

Java 如何在三元运算符中使用枚举检查两个条件?,java,enums,ternary-operator,Java,Enums,Ternary Operator,我有一个枚举: public enum GSProccesingType { bigCover, cover, other } 这种情况如果封面出现,则选择FileStoreUtils.coverFileName,如果bigcolver出现,则选择bigcolvefilename否则为“04d.png” 我不能对掩护和大掩护提出一个简短的条件 final String fileName = proccesingType == cover ? FileStoreUtil

我有一个枚举:

public enum GSProccesingType {
    bigCover,
    cover,
    other
}
这种情况如果封面出现,则选择
FileStoreUtils.coverFileName
,如果
bigcolver
出现,则选择
bigcolvefilename
否则为“04d.png”

我不能对掩护和大掩护提出一个简短的条件

final String fileName = proccesingType == cover  ? FileStoreUtils.coverFileName : "%04d.png";

final String fileName2 = proccesingType == bigCover ? FileStoreUtils.bigCoverFileName : "04d.png";

如何将两行连接到一行才能正确?

可以使用如下嵌套的三元运算符:

final String fileName = proccesingType == cover  ? 
                            FileStoreUtils.coverFileName : 
                            (proccesingType == bigCover ? FileStoreUtils.bigCoverFileName : "%04d.png");
,但这很难理解。我建议为此创建一种方法:

private String getFilename(GSProccesingType type) {
    switch(type) {
        case cover: return FileStoreUtils.coverFileName;
        case bigCover : return FileStoreUtils.bigCoverFileName ;
        default: return "%04d.png";
    }
}
这本书更长,但更容易阅读和理解


我还建议如下,并使用枚举常量的所有大写字母。

请将您的代码作为text发布使用
如果elseif
语句或
嵌套?:
运算符。请阅读并接受答案