C# 枚举值设置为不同的数字/重置枚举值
我正在尝试创建一个程序,根据您提供的起始音符计算不同的音阶 我对不同的音符进行了枚举:C# 枚举值设置为不同的数字/重置枚举值,c#,enums,numbers,int,C#,Enums,Numbers,Int,我正在尝试创建一个程序,根据您提供的起始音符计算不同的音阶 我对不同的音符进行了枚举: public enum NoteValue { A = 0, Asharp = 1, B = 2, C = 3, Csharp = 4, D = 5, Dsharp = 6, E = 7, F = 8, Fsharp = 9, G = 10, Gsharp = 11 } 然后我有了一个设置每个音符的方法
public enum NoteValue
{
A = 0,
Asharp = 1,
B = 2,
C = 3,
Csharp = 4,
D = 5,
Dsharp = 6,
E = 7,
F = 8,
Fsharp = 9,
G = 10,
Gsharp = 11
}
然后我有了一个设置每个音符的方法
public void setNotes(NoteValue startingNote)
{
//Creates an array of notes the size that is specified
theNote = new Note[(numberOfNotes)];
//Sets the notes
theNote[0] = new Note(startingNote);
theNote[1] = new Note((startingNote + step[1]));
theNote[2] = new Note((startingNote + step[2] + step[1]));
theNote[3] = new Note((startingNote + step[3] + step[2] + step[1]));
theNote[4] = new Note((startingNote + step[4] + step[3] + step[2] + step[1]));
theNote[5] = new Note((startingNote + step[5] + step[4] + step[3] + step[2] + step[1]));
theNote[6] = new Note((startingNote - step[7]));
Console.WriteLine("{0} \n{1} \n{2} \n{3} \n{4} \n{5} \n{6}",
theNote[0].value, theNote[1].value, theNote[2].value, theNote[3].value,
theNote[4].value, theNote[5].value, theNote[6].value);
}
我遇到的问题是,如果它发生了,我从G开始(在我的枚举中是10),
它将在G#之后开始打印数字。我能把它弄回来吗
11点后回到0,而不是继续
我会得到这样的东西(大音阶):
G
12
14
15
17
十九,
而不是
G
A.
B
C
D
E
F#
有办法解决这个问题吗?谢谢。C#中定义的枚举基本上是整型(整数)的“强类型”包装 如果希望整数具有这种包装行为,常用的解决方案是使用模(
%
)运算符:
int note = 12;
var correctlyWrappedNote = note % 12; // will equal 0
这在逻辑上相当于在除以12后取余数
然后,您应该能够将其转换回您的NoteValue
类型:
var actualNote = (NoteValue)correctlyWrappedNote;
如果你给模加一个负数,你会得到一个负数。如果你必须处理负数,那么还有一个额外的步骤:
int note = -1;
var correctlyWrappedNote = note % 12; // will equal -1
if (correctlyWrappedNote < 0)
correctlyWrappedNote = 12 + correctlyWrappedNote; // will equal 11
var actualNote = (NoteValue)correctlyWrappedNote; // Will equal Gsharp
int note=-1;
var correctlyWrappedNote=注释%12;//将等于-1
如果(正确包装说明<0)
correctlyWrappedNote=12+correctlyWrappedNote;//将等于11
var actualNote=(NoteValue)correctlyWrappedNote;//将等于Gsharp
C#中定义的枚举基本上是整型(整数)的“强类型”包装器
如果希望整数具有这种包装行为,常用的解决方案是使用模(%
)运算符:
int note = 12;
var correctlyWrappedNote = note % 12; // will equal 0
这在逻辑上相当于在除以12后取余数
然后,您应该能够将其转换回您的NoteValue
类型:
var actualNote = (NoteValue)correctlyWrappedNote;
如果你给模加一个负数,你会得到一个负数。如果你必须处理负数,那么还有一个额外的步骤:
int note = -1;
var correctlyWrappedNote = note % 12; // will equal -1
if (correctlyWrappedNote < 0)
correctlyWrappedNote = 12 + correctlyWrappedNote; // will equal 11
var actualNote = (NoteValue)correctlyWrappedNote; // Will equal Gsharp
int note=-1;
var correctlyWrappedNote=注释%12;//将等于-1
如果(正确包装说明<0)
correctlyWrappedNote=12+correctlyWrappedNote;//将等于11
var actualNote=(NoteValue)correctlyWrappedNote;//将等于Gsharp
@Merlyn的答案包含了您需要做的事情的要点,但是使用%12
仅仅因为您的枚举成员正好是12,并且因为每个数字0到11都分配给一个枚举成员,这是一个分解代码的方法。为了让它更适应变化,你可以这样写
var notes = Enum.GetValues(typeof(NoteValue)); //array
var startingNote = Array.IndexOf(notes,NoteValue.Fsharp);//8
var fourNotesAfterStartingNote = notes[(startingNote+4)%notes.Length];//Asharp
即使添加了新注释,上述代码也将继续正常工作。可能不太可能-但代码总是会更改:)@Merlyn的答案包含了您需要执行的操作的要点,但是使用
%12
仅仅因为您的枚举成员正好是12,并且因为每个数字0到11都分配给一个枚举成员,这是一个导致代码中断的方法。为了让它更适应变化,你可以这样写
var notes = Enum.GetValues(typeof(NoteValue)); //array
var startingNote = Array.IndexOf(notes,NoteValue.Fsharp);//8
var fourNotesAfterStartingNote = notes[(startingNote+4)%notes.Length];//Asharp
即使添加了新注释,上述代码也将继续正常工作。可能不太可能-但代码总是会更改:),但你是对的-最好不要使用幻数,并且根据逻辑运算(音符计数)而不是哨兵值进行计算。非常有说服力地说。我想从现在开始我会使用确切的词语:),但你是对的-这是一个很好的做法,永远不要使用幻数,并且根据逻辑运算(音符计数)而不是哨兵值进行计算。非常雄辩地说。我想从现在起,我会使用确切的词语:)