C# while 循环由于某种原因不起作用
本文关键字:不起作用 由于某种原因 循环 while | 更新日期: 2023-09-27 17:56:47
我确定这只是我疲惫的眼睛看不到的一些愚蠢的东西,所以请看一下:
int NoteCounter = 0;
int MaxNotes = 100;
string SongSegment = "";
Random NoteIndexGen = new Random();
while (NoteCounter <= MaxNotes)
{
int NoteIndex = NoteIndexGen.Next(1, 7);
#region SetNotes
switch (Key)
{
case "C-Maj":
switch (NoteIndex)
{
case 1:
SongSegment += " C ";
OutputInfo1.Text = SongSegment;
break;
case 2:
SongSegment += " D ";
OutputInfo1.Text = SongSegment;
break;
case 3:
SongSegment += " E ";
OutputInfo1.Text = SongSegment;
break;
case 4:
SongSegment += " F ";
OutputInfo1.Text = SongSegment;
break;
case 5:
SongSegment += " G ";
OutputInfo1.Text = SongSegment;
break;
case 6:
SongSegment += " A ";
OutputInfo1.Text = SongSegment;
break;
case 7:
SongSegment += " B ";
OutputInfo1.Text = SongSegment;
break;
}
break;
case "G-Mag":
switch (NoteIndex)
{
case 1:
SongSegment += " G ";
OutputInfo1.Text = SongSegment;
break;
case 2:
SongSegment += " A ";
OutputInfo1.Text = SongSegment;
break;
case 3:
SongSegment += " B ";
OutputInfo1.Text = SongSegment;
break;
case 4:
SongSegment += " C ";
OutputInfo1.Text = SongSegment;
break;
case 5:
SongSegment += " D ";
OutputInfo1.Text = SongSegment;
break;
case 6:
SongSegment += " E ";
OutputInfo1.Text = SongSegment;
break;
case 7:
SongSegment += " F# ";
OutputInfo1.Text = SongSegment;
break;
}
break;
case "D-Maj":
switch (NoteIndex)
{
case 1:
SongSegment += " D ";
OutputInfo1.Text = SongSegment;
break;
case 2:
SongSegment += " E ";
OutputInfo1.Text = SongSegment;
break;
case 3:
SongSegment += " F# ";
OutputInfo1.Text = SongSegment;
break;
case 4:
SongSegment += " G ";
OutputInfo1.Text = SongSegment;
break;
case 5:
SongSegment += " A ";
OutputInfo1.Text = SongSegment;
break;
case 6:
SongSegment += " B ";
OutputInfo1.Text = SongSegment;
break;
case 7:
SongSegment += " C# ";
OutputInfo1.Text = SongSegment;
break;
}
break;
case "A-Maj":
switch (NoteIndex)
{
case 1:
SongSegment += " A ";
OutputInfo1.Text = SongSegment;
break;
case 2:
SongSegment += " B ";
OutputInfo1.Text = SongSegment;
break;
case 3:
SongSegment += " C# ";
OutputInfo1.Text = SongSegment;
break;
case 4:
SongSegment += " D ";
OutputInfo1.Text = SongSegment;
break;
case 5:
SongSegment += " E ";
OutputInfo1.Text = SongSegment;
break;
case 6:
SongSegment += " F# ";
OutputInfo1.Text = SongSegment;
break;
case 7:
SongSegment += " G# ";
OutputInfo1.Text = SongSegment;
break;
}
break;
case "E-Maj":
switch (NoteIndex)
{
case 1:
SongSegment += " E ";
OutputInfo1.Text = SongSegment;
break;
case 2:
SongSegment += " F# ";
OutputInfo1.Text = SongSegment;
break;
case 3:
SongSegment += " G# ";
OutputInfo1.Text = SongSegment;
break;
case 4:
SongSegment += " A ";
OutputInfo1.Text = SongSegment;
break;
case 5:
SongSegment += " B ";
OutputInfo1.Text = SongSegment;
break;
case 6:
SongSegment += " C# ";
OutputInfo1.Text = SongSegment;
break;
case 7:
SongSegment += " D# ";
OutputInfo1.Text = SongSegment;
break;
}
break;
}
NoteCounter++;
#endregion SetNotes
#endregion GenerateMusic
break;
}
您可能已经发现我正在制作一个随机音乐生成器之类的东西,稍后我将对其进行扩展,但是为了使它起作用,我需要将音符添加到字符串SongSegment
RichTextBox
中的输出只是一个音符,如下所示:
A
看到我没抓到的东西了吗?
var SongSegments = new Dictionary<char, string[]>() {
{ 'C', "C D E F G A B".Split(' ') },
{ 'G', "G A B C D E F#".Split(' ') },
{ 'D', "D E F# G A B C#".Split(' ') },
{ 'A', "A B C# D E F# G#".Split(' ') },
{ 'E', "E F# G# A B C# D#".Split(' ') } };
OutputInfo1.Text = string.Concat(Enumerable.Repeat(new Random(), 100)
.Select(r => " " + SongSegments[Key[0]][r.Next(0, 6)] + " "));
正如上面的评论中提到的@Ken Y-N,删除最后一条break
语句。这打破了 while 循环,意味着循环最多执行一次。
ppl 是正确的,最终break;
是错误的,我认为您的代码也需要一些优化:
int NoteCounter = 0;
int MaxNotes = 100;
string SongSegment = "";
Random NoteIndexGen = new Random();
while (NoteCounter <= MaxNotes)
{
int NoteIndex = NoteIndexGen.Next(1, 7);
#region SetNotes
switch (Key)
{
case "C-Maj":
switch (NoteIndex)
{
case 1:
SongSegment += " C ";
break;
case 2:
SongSegment += " D ";
break;
case 3:
SongSegment += " E ";
break;
case 4:
SongSegment += " F ";
break;
case 5:
SongSegment += " G ";
break;
case 6:
SongSegment += " A ";
break;
case 7:
SongSegment += " B ";
break;
}
OutputInfo1.Text = SongSegment;
break;
case "G-Mag":
switch (NoteIndex)
{
case 1:
SongSegment += " G ";
break;
case 2:
SongSegment += " A ";
break;
case 3:
SongSegment += " B ";
break;
case 4:
SongSegment += " C ";
break;
case 5:
SongSegment += " D ";
break;
case 6:
SongSegment += " E ";
break;
case 7:
SongSegment += " F# ";
break;
}
OutputInfo1.Text = SongSegment;
break;
case "D-Maj":
switch (NoteIndex)
{
case 1:
SongSegment += " D ";
break;
case 2:
SongSegment += " E ";
break;
case 3:
SongSegment += " F# ";
break;
case 4:
SongSegment += " G ";
break;
case 5:
SongSegment += " A ";
break;
case 6:
SongSegment += " B ";
break;
case 7:
SongSegment += " C# ";
break;
}
OutputInfo1.Text = SongSegment;
break;
case "A-Maj":
switch (NoteIndex)
{
case 1:
SongSegment += " A ";
break;
case 2:
SongSegment += " B ";
break;
case 3:
SongSegment += " C# ";
break;
case 4:
SongSegment += " D ";
break;
case 5:
SongSegment += " E ";
break;
case 6:
SongSegment += " F# ";
break;
case 7:
SongSegment += " G# ";
break;
}
OutputInfo1.Text = SongSegment;
break;
case "E-Maj":
switch (NoteIndex)
{
case 1:
SongSegment += " E ";
break;
case 2:
SongSegment += " F# ";
break;
case 3:
SongSegment += " G# ";
break;
case 4:
SongSegment += " A ";
break;
case 5:
SongSegment += " B ";
break;
case 6:
SongSegment += " C# ";
break;
case 7:
SongSegment += " D# ";
break;
}
OutputInfo1.Text = SongSegment;
break;
}
NoteCounter++;
#endregion SetNotes
#endregion GenerateMusic
}
我认为
这是因为您正在使用休息时间; 最后多了一个时间....
int NoteCounter = 0;
int MaxNotes = 100;
string SongSegment = "";
Random NoteIndexGen = new Random();
while (NoteCounter <= MaxNotes)
{
int NoteIndex = NoteIndexGen.Next(1, 7);
#region SetNotes
switch (Key)
{
case "C-Maj":
switch (NoteIndex)
{
case 1:
SongSegment += " C ";
OutputInfo1.Text = SongSegment;
break;
case 2:
SongSegment += " D ";
OutputInfo1.Text = SongSegment;
break;
case 3:
SongSegment += " E ";
OutputInfo1.Text = SongSegment;
break;
case 4:
SongSegment += " F ";
OutputInfo1.Text = SongSegment;
break;
case 5:
SongSegment += " G ";
OutputInfo1.Text = SongSegment;
break;
case 6:
SongSegment += " A ";
OutputInfo1.Text = SongSegment;
break;
case 7:
SongSegment += " B ";
OutputInfo1.Text = SongSegment;
break;
}
break;
case "G-Mag":
switch (NoteIndex)
{
case 1:
SongSegment += " G ";
OutputInfo1.Text = SongSegment;
break;
case 2:
SongSegment += " A ";
OutputInfo1.Text = SongSegment;
break;
case 3:
SongSegment += " B ";
OutputInfo1.Text = SongSegment;
break;
case 4:
SongSegment += " C ";
OutputInfo1.Text = SongSegment;
break;
case 5:
SongSegment += " D ";
OutputInfo1.Text = SongSegment;
break;
case 6:
SongSegment += " E ";
OutputInfo1.Text = SongSegment;
break;
case 7:
SongSegment += " F# ";
OutputInfo1.Text = SongSegment;
break;
}
break;
case "D-Maj":
switch (NoteIndex)
{
case 1:
SongSegment += " D ";
OutputInfo1.Text = SongSegment;
break;
case 2:
SongSegment += " E ";
OutputInfo1.Text = SongSegment;
break;
case 3:
SongSegment += " F# ";
OutputInfo1.Text = SongSegment;
break;
case 4:
SongSegment += " G ";
OutputInfo1.Text = SongSegment;
break;
case 5:
SongSegment += " A ";
OutputInfo1.Text = SongSegment;
break;
case 6:
SongSegment += " B ";
OutputInfo1.Text = SongSegment;
break;
case 7:
SongSegment += " C# ";
OutputInfo1.Text = SongSegment;
break;
}
break;
case "A-Maj":
switch (NoteIndex)
{
case 1:
SongSegment += " A ";
OutputInfo1.Text = SongSegment;
break;
case 2:
SongSegment += " B ";
OutputInfo1.Text = SongSegment;
break;
case 3:
SongSegment += " C# ";
OutputInfo1.Text = SongSegment;
break;
case 4:
SongSegment += " D ";
OutputInfo1.Text = SongSegment;
break;
case 5:
SongSegment += " E ";
OutputInfo1.Text = SongSegment;
break;
case 6:
SongSegment += " F# ";
OutputInfo1.Text = SongSegment;
break;
case 7:
SongSegment += " G# ";
OutputInfo1.Text = SongSegment;
break;
}
break;
case "E-Maj":
switch (NoteIndex)
{
case 1:
SongSegment += " E ";
OutputInfo1.Text = SongSegment;
break;
case 2:
SongSegment += " F# ";
OutputInfo1.Text = SongSegment;
break;
case 3:
SongSegment += " G# ";
OutputInfo1.Text = SongSegment;
break;
case 4:
SongSegment += " A ";
OutputInfo1.Text = SongSegment;
break;
case 5:
SongSegment += " B ";
OutputInfo1.Text = SongSegment;
break;
case 6:
SongSegment += " C# ";
OutputInfo1.Text = SongSegment;
break;
case 7:
SongSegment += " D# ";
OutputInfo1.Text = SongSegment;
break;
}
break;
}
NoteCounter++;
#endregion SetNotes
#endregion GenerateMusic
}