2016-07-13 13 views
-2

のために働いていない間、私は、これはちょうど私の疲れた目にはそう見てください見ていないいくつかの愚かなことだと確信している:C#のループが何らかの理由

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 

私がキャッチしなかったものを見ますか?

+4

あなたの最後の '休憩を。 'を削除する必要があります。 –

+3

あなたの目は、過度の押し込みのためにおそらく疲れています。 –

+1

デバッガ(ここに投稿した不完全でコンパイル不可能な抜粋ではなく、実際のコードで使用されています)は、これをかなり早く把握できるはずです。実際の[mcve]を提供していないので、特に恐ろしくインデントされた部分的なコードを提供すると、私たちの言うことはかなり難しいです。 –

答えて

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

ありがとう、スイッチのステートメントは痛みがあり、私は過度にそれらを使用しており、代替案は大いに感謝しています:) –

1

上記のコメントに記載されている@Ken Y-Nは、最後のbreakステートメントを削除します。これはwhileループから抜け出し、ループがたかだか1回実行されることを意味します。

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

皆さんが真のエンドbreak;は間違っていると、あなたのコードOFCすぎだと思ういくつかの最適化を必要とします:

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 

} 
関連する問題