2017-01-21 34 views
0

错误这是我的C#代码和我得到这个错误:名称“viesti_s”不会在目前情况下试图将一个字符串中插入字符循环得到C#

存在这里是代码:

 string viesti_i = Console.ReadLine(); 
     Console.WriteLine("give number "); 
     int avain = int.Parse(Console.ReadLine()); 

     for (int i = 0; i < viesti_i.Length; i += avain) 
     { 
      string viesti_s = viesti_i.Insert(avain, "b"); 
     } 

     Console.WriteLine(viesti_s); 
     Console.ReadKey(); 
+0

因为你没有viesti_s变量声明,你有viesti_i可变 –

+0

要么宣布_before_循环变量,所以你以后可以访问它,或移动'Console.WriteLine()'内循环。你试图达到的结果很难。 –

回答

1
Console.WriteLine("give number "); 
int avain = int.Parse(Console.ReadLine()); 

string viesti_s = "HELLO !"; 

for (int i = 0; i < viesti_i.Length; i += avain) 
{ 
    viesti_s = viesti_i.Insert(avain, "b"); 
} 

Console.WriteLine(viesti_s); 
Console.ReadKey(); 
+1

请详细说明您的答案并解释代码的作用,因为仅用于代码的答案可能难以理解。 – David

+0

我觉得这很明显。他在for循环之前声明了字符串viesti_s,然后在for循环中引用它。让它在for循环之外允许他在声明后的任何时候引用它。在for循环中声明的任何东西都是for循环所独有的。 – rohanharrison