C#while循环由于某种原因不起作用

时间:2016-07-13 23:57:50

标签: c# while-loop

我确定这只是一些愚蠢的事情,我的疲惫的眼睛看不到所以请看看:

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 < / p>

RichTextBox中的输出只是一个音符,如下所示:

  A  

看到我没有抓到的任何东西?

4 个答案:

答案 0 :(得分:2)

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)] + "  "));

答案 1 :(得分:1)

如上面评论中提到的@Ken Y-N,删除最后一个break语句。这突破了while循环,意味着循环最多只执行一次。

答案 2 :(得分:1)

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

}

答案 3 :(得分:0)

我认为这是因为你正在使用休息;最后加时间......

                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
                }