2012-01-16 10 views
0

このプログラムをデバッグしてエラーを検出しましたが、そのエラーを検出できませんでした。何らかの理由で、この行に範囲外のエラー配列インデックスが表示されています。 moves [nCount] .sDirection = sStep [0];私は知っている、このフォーラムはデバッグのためのものではありません、それは申し訳ありません。ReadLine - 配列のインデックスが範囲外にある

 class Program 
{ 
    struct move 
    { 
     public char sDirection; 
     public int steps; 
    } 
    static void Main(string[] args) 
    { 
     int nNumOfInstructions = 0; 
     int nStartX = 0, nStartY = 0; 
     move[] moves = new move[nNumOfInstructions]; 


     nNumOfInstructions=Convert.ToInt32(Console.ReadLine()); 


     string sPosCoOrd = Console.ReadLine(); 
     nStartX = Convert.ToInt32(sPosCoOrd[0]); 

     nStartY = Convert.ToInt32(sPosCoOrd[2]); 

     string sStep = ""; 

     for (int nCount = 0; nCount < nNumOfInstructions; nCount++) 
     { 
      sStep = Console.ReadLine(); 
      int length = sStep.Length; 
      moves[nCount].sDirection = sStep[0]; 
      moves[nCount].steps = Convert.ToInt32(sStep[1]); 


     } 


     Console.ReadLine(); 
    } 
} 

答えて

1

コードでは、moves配列は長さゼロの配列として作成されます。いずれの指標については、この配列にアクセスすると、必然的にあなたは、おそらくこのようにそれをしたい境界

1

のうち、配列インデックスがスローされます。

class Program 
{ 
    struct move 
    { 
     public char sDirection; 
     public int steps; 
    } 

    static void Main(string[] args) 
    { 
     int nNumOfInstructions = Convert.ToInt32(Console.ReadLine()); 
     move[] moves = new move[nNumOfInstructions]; 

     string sPosCoOrd = Console.ReadLine(); 
     int nStartX = Convert.ToInt32(sPosCoOrd[0]); 
     int nStartY = Convert.ToInt32(sPosCoOrd[2]); 

     string sStep = String.Empty; 

     for (int nCount = 0; nCount < nNumOfInstructions; nCount++) 
     { 
      sStep = Console.ReadLine(); 
      int length = sStep.Length; 
      moves[nCount].sDirection = sStep[0]; 
      moves[nCount].steps = Convert.ToInt32(sStep[1]); 
     } 
    } 
} 
関連する問題