日期:2014-05-18  浏览次数:20860 次

初学者求教return的一些问题
下面是添加的一个类
class Encrypt
  {
  public string ToEncrypt(string sourceStr, string keyStr)
  {
  StringBuilder cyrpt = new StringBuilder();
  if (sourceStr.Length != keyStr.Length)
  Console.WriteLine("密钥字符串需跟加密字符串长度相同");
  else
  {
  for (int i = 0; i < sourceStr.Length; i++)
  {
  cyrpt.Append((char)(sourceStr[i]^keyStr[i]));
  }
  }
  return cyrpt.ToString();  
  }
然后主程序
static void Main(string[] args)
  {
  Encrypt main = new Encrypt();
  main.ToEncrypt(Console.ReadLine(),Console.ReadLine());  
  }
怎样才能显示 Encrypt类中return回去的那个值


------解决方案--------------------
Console.WriteLine(main.ToEncrypt(Console.ReadLine(),Console.ReadLine()));
这个意思?
------解决方案--------------------
class Encrypt
{
public string ToEncrypt(string sourceStr, string keyStr)
{
StringBuilder cyrpt = new StringBuilder();
if (sourceStr.Length != keyStr.Length)
throw new Exception("密钥字符串需跟加密字符串长度相同");
else
{
for (int i = 0; i < sourceStr.Length; i++)
{
cyrpt.Append((char)(sourceStr[i]^keyStr[i]));
}
}
return cyrpt.ToString();
}

static void Main(string[] args)
{
Encrypt main = new Encrypt();
bool isSucceed = false;
while (!isSucceed)
{
try
{
main.ToEncrypt(Console.ReadLine(),Console.ReadLine());
isSucceed = true;
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);

}

------解决方案--------------------
string s=main.ToEncrypt(Console.ReadLine(),Console.ReadLine());
------解决方案--------------------
string test =main.ToEncrypt(Console.ReadLine(),Console.ReadLine()); //test就是回来的那个值