C# 4.0 ReadLine-数组索引超出范围
我一直在调试这个程序以找到错误,但没有成功。出于某种原因,它在此行中显示了一个错误数组索引超出范围 移动[nCount].sddirection=sStep[0];我知道,这个论坛不是用来调试的,对此我很抱歉C# 4.0 ReadLine-数组索引超出范围,c#-4.0,C# 4.0,我一直在调试这个程序以找到错误,但没有成功。出于某种原因,它在此行中显示了一个错误数组索引超出范围 移动[nCount].sddirection=sStep[0];我知道,这个论坛不是用来调试的,对此我很抱歉 class Program { struct move { public char sDirection; public int steps; } static void Main(string[] args)
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();
}
}
类程序
{
结构移动
{
公共字符方向;
公共int步骤;
}
静态void Main(字符串[]参数)
{
int nnumof指令=0;
int-nStartX=0,nStartY=0;
移动[]移动=新移动[nNumOfInstructions];
nNumOfInstructions=Convert.ToInt32(Console.ReadLine());
字符串sPosCoOrd=Console.ReadLine();
nStartX=转换为32(sposocord[0]);
nStartY=Convert.ToInt32(sposocord[2]);
字符串sStep=“”;
for(int-nCount=0;nCount
在您的代码中,移动
数组被创建为长度为零的数组。对于任何索引,访问此数组都不可避免地会使数组索引超出范围您可能希望这样做:
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]);
}
}
}
类程序
{
结构移动
{
公共字符方向;
公共int步骤;
}
静态void Main(字符串[]参数)
{
int nNumOfInstructions=Convert.ToInt32(Console.ReadLine());
移动[]移动=新移动[nNumOfInstructions];
字符串sPosCoOrd=Console.ReadLine();
int nStartX=Convert.ToInt32(sposocord[0]);
int nStartY=Convert.ToInt32(sposocord[2]);
string-sStep=string.Empty;
for(int-nCount=0;nCount