日期:2014-05-16  浏览次数:20876 次

写了一个很入门的程序,报错说数组越标。。。?请教foreach的用法
static void Main(string[] args)
        {
            Console.WriteLine("Pls entry one string");
            string userStr=Console.ReadLine();
            char[] userChar = userStr.ToCharArray();
            char[] aimChar=new char[userChar.Length] ;
            string aimString = "";
            int i=0;
           
            foreach(char userSingChar in userChar)
            {
                
              aimChar[userChar.Length-i]=userSingChar;
              i++;
            }
            foreach(char aimSingChar in aimChar )
            {
                    aimString = aimString + aimSingChar;
            }

            Console.WriteLine(aimString);
            Console.ReadKey();
        }
------解决方案--------------------
  foreach(char userSingChar in userChar)
             {
                 
               aimChar[userChar.Length - i - 1]=userSingChar;
               i++;
             }

这样就对了.......