0

首先,我的代码(它远非完美,我真的不知道我在做什么)是这样的:

   public enum Chord { MAJOR, MINOR, DIMINISHED, BASS, BASS2 }
   public enum Scales { C, D, E, F, G, A }
public class EnumTest
{
Chord chord;
public EnumTest(Chord chord)
{
    this.chord = chord;
}
public void tellItLikeItIs()
{

switch (chord) {

    case MAJOR:
            for(Scales C : Scales.values())
                System.out.println(C + " " + C.ordinal());
                break;

//I've tried in the CHORD enum going MAJOR(0, 2, 4) but I don't think that was correct
    case MINOR: System.out.println("C, Eb, G");
                break;
    default:
        System.out.println("I screwed up");
        break;

}
}


public static void main(String[] args)
{

EnumTest firstDay = new EnumTest(Chord.MAJOR);
firstDay.tellItLikeItIs();
EnumTest thirdDay = new EnumTest(Chord.MINOR);
thirdDay.tellItLikeItIs();
System.out.println("Here are all Scale degrees" +
                   " and their ordinal values: ");
for(Scales C : Scales.values())
  System.out.println(C + " " + C.ordinal());



}

}

我可能遗漏了一些括号和东西,我无法使用代码工具发布其中的一些内容。我的问题是,对于主要情况,我可以让编译器打印 C 0、D 1、E 2 等。除了我只希望它打印 C、E 和 G(0、2、4)。有没有办法只为大和弦选择这 3 个序数值并打印出来?

此外,在 Scales 枚举中,我还需要锐器(C、C#、D、D#..),除了锐器是“非法字符”,_MusicChord\Scales.java:2: illegal character: \35我试图查看转义字符,但我要么不理解这篇文章我读了或者我看错了东西。有人还可以告诉我如何将#'s 添加到 Scales 类中而不会使它们成为非法字符吗?任何帮助表示赞赏

4

5 回答 5

2

In the following example you can see how you could address some of the problems you are facing:

public class EnumTest
{
    public enum Chord
    {
        MAJOR,
        MINOR,
        DIMINISHED,
        BASS,
        BASS2
    }

    public enum Scales
    {
        C("C"),
        CSharp("C#"),
        E("E"),
        F("F"),
        G("G"),
        A("A");

        private final String printName;

        Scales(String printName)
        {
            this.printName = printName;
        }

        public String getPrintName()
        {
            return printName;
        }

        public String toString()
        {
            return printName;
        }

        private static final Map<Chord, List<Scales>> scalesForChord;

        static
        {
            scalesForChord = new HashMap<Chord, List<Scales>>();

            scalesForChord.put(Chord.MAJOR, Arrays.asList(Scales.C, Scales.E, Scales.G));
        }

        public static List<Scales> getScalesForChord(Chord chord)
        {
            return scalesForChord.get(chord);
        }
    }
}
于 2011-11-04T13:00:48.113 回答
1

除了其他答案之外,您不应该使用 .ordinal()它,因为它会使您的代码在未来更难扩展。示例:如果您必须添加一个枚举并且它的序数不是您需要的,该怎么办?

序数是 ENUM 在其声明中的“位置”。在您的示例中,这恰好与您的域在语义上有意义的内容相吻合。

通过让构造函数为各个 ENUM 获取这些值来引起对这种语义重要性的注意是一个更好的主意,就像在 Gandalf 的示例中一样。代替C("C"),have C("C", 1),并且除了final String printName;还拥有一个final String whateverThatNumberMeans;

于 2011-11-04T13:06:27.323 回答
0

试试这个为你的案例主要:

case MAJOR:
            for (Scales c : EnumSet.of(Scales.C, Scales.E, Scales.G)) {
                System.out.println(c + " " + c.ordinal());
            }
            break;

这对于您的 Scales 枚举:

public enum Scales {
    C, Cs("C#"), D, Ds("D#"), E, F, G, A;
    private final String label;
    private Scales() {
        this.label = name();
    }
    private Scales(String label) {
        this.label = label;
    }
    @Override
    public String toString() { return label; }
}
于 2011-11-04T13:12:12.393 回答
0

在循环中仅选择枚举使用if(c.ordinal() == 1)或更好的相关元素。if(Chord.MAJOR.equals(c))我希望我理解你的问题。

第二个问题不是很清楚。如果您想#用作标识符名称的一部分,请不要这是不可能的。这个字符是被禁止的。但是,如果您愿意,可以将其用于toString()实现。

我希望这有帮助。

于 2011-11-04T12:54:42.557 回答
0

做{ C, Cs, D, ..., A, As, B }。同样,您也可以执行 { C, Db, D, ..., A, Bb, B }。作为一项规则,坚持使用[[:alpha:]_][[:alnum:]_]*名称。

于 2011-11-04T12:55:05.483 回答