如何使用另一个枚举选择特定的 enum.ordinal() ?

发布于 2024-12-13 23:12:36 字数 1418 浏览 0 评论 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: 非法字符: \35< /code> 我试图研究转义字符,但我要么不理解我读的文章,要么我看错了东西。有人还可以告诉我如何将 # 添加到 Scales 类中而不使它们成为非法字符吗?任何帮助表示赞赏

First, my code (It is far from perfect, I don't really know what I am doing) is this:

   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());



}

}

I might be missing a few brackets and things, I had trouble posting some of it using the code tool. My question is, for case MAJOR, I can get the compiler to print C 0, D 1, E 2, etc.. except I only want it to print C, E and G (0, 2, 4). Is there a way to select ONLY these 3 ordinal values for a major chord and print those?

Also, in the Scales enum I also need the sharps (C, C#, D, D#..) except the sharps are 'illegal characters' and I get _MusicChord\Scales.java:2: illegal character: \35 I tried to look into escape characters, but I either didn't understand the article I read or I was looking at the wrong thing. Could someone also tell me how to add the #'s into the Scales class without them being illegal characters? Any help is appreciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

治碍 2024-12-20 23:12:36

在以下示例中,您可以了解如何解决您面临的一些问题:

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);
        }
    }
}

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);
        }
    }
}
小ぇ时光︴ 2024-12-20 23:12:36

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

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

最好通过使用采用这些值的构造函数来引起人们对这种语义重要性的注意对于各个 ENUM,就像甘道夫的例子一样。代替 C("C"), 具有 C("C", 1),并且除了 final String printName; 之外,还有一个最终字符串whateverThatNumberMeans;

In addition to the other answers, you should not use .ordinal() because it makes your code harder to extend in the future. Example: What if you have to add an enum and its ordinal is not what you need it it be?

The ordinal is the 'position' of the ENUM in its declaration. In your example this happens to coincide with something that semantically makes sense to your domain.

It is a better idea to draw attention to this semantic importance by having constructors that take these values for the individual ENUMs, like in Gandalf's example. Instead of C("C"), have C("C", 1), and in addition to final String printName; also have a final String whateverThatNumberMeans;

呆萌少年 2024-12-20 23:12:36

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

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

我希望这有帮助。

To select only relevant elements of enum use if(c.ordinal() == 1) or better if(Chord.MAJOR.equals(c)) in loop. I hope I understood your question.

The second question is not very clear. If you want to use # as a part of identifier name please not it is impossible. This character is forbidden. But you can use it into toString() implementation if you want.

I hope this helps.

橘和柠 2024-12-20 23:12:36

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

Do { C, Cs, D, ..., A, As, B }. Similarly, you could also do { C, Db, D, ..., A, Bb, B }. As a rule, stick with [[:alpha:]_][[:alnum:]_]* for names.

老娘不死你永远是小三 2024-12-20 23:12:36

为您的案例 MAJOR 尝试此操作:

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; }
}

Try this for your case MAJOR:

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

and this for your Scales enum:

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; }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文