本人初学C#,编写了一小段程序,但始终无法调试通过,计算机总是提示“索引超出了数组界限。”憋得很苦。希望大家能给我一个答案。在此表示感谢。下面是这段程序,是有关于“字符倒置”的:
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string arg =Console .ReadLine();
char[] argss = arg.ToCharArray();
int i = 0;
char[] temp = new char[10];
for (int j = argss.Length; j >= 0; j--)
{ temp[i] = argss[j-1];
i++;
}
for (i = 0; i < argss.Length; i++)
Console.WriteLine(temp[i]);
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string arg =Console .ReadLine();
char[] argss = arg.ToCharArray();
int i = 0;
char[] temp = new char[10];
for (int j = argss.Length; j >= 0; j--)
{ temp[i] = argss[j-1];
i++;
}
for (i = 0; i < argss.Length; i++)
Console.WriteLine(temp[i]);
}
}
}
{ temp[i] = argss[j-1];
i++;
}
for (i = 0; i < argss.Length - 1; i++)
Console.WriteLine(temp[i]);
当j=0,这句temp[i] = argss[j-1];0-1=-1,就有exceptionfor (int j = argss.Length - 1; j >= 1; j--)
{ temp[i] = argss[j-1];
i++;
}
for (i = 0; i < argss.Length - 1; i++)
Console.WriteLine(temp[i]);
for(int j=arr.length-1;j>=0;j--)
{
temp[i++]=argss[j];
}