日期:2014-05-17  浏览次数:21212 次

如何更改字符数组中的值
问题:随机读取用户输入的一个字符串,然后对其加密,加密后的字符是原字符串中对应字符的值+3,如用户输入"ABC",那么加密后字符串应为“DEF”.下面是我的程序,但是存在语法错误,无法运行,请老师指点:
using System;
using System.Collections.Generic;
using System.Text;

namespace ex4_5
{
  class EncryCode
  {
  static void Main(string[] args)
  {
  char[] s2 ;
  Console.WriteLine("Please enter a string.");
  string s1 = Console.ReadLine();
  s2 = s1.ToCharArray();
  for (int i = 0; i < s2.Length; i++)
  s2[i] = s2[i] + 3; //编译的时候,提示无法将类型“int”隐式转换为“char”
  Console.Write("Encried's string is ");
  for (int i = 0; i < s2.Length; i++)
  Console.Write("{0}", s2[i]);


  }
  }
}


------解决方案--------------------
s2[i] = (char)(Convert.ToInt32(s2[i]) + 3);

不知道行不行。