简体   繁体   English

C#错误CS0103:名称“ i”在当前上下文中不存在

[英]C# Error CS0103: The name 'i' does not exist in the current context

Can someone give me some insight on my code. 有人可以给我一些有关我的代码的见解。 My C# program is suppose to transmit a message from a client to a server but extract and print only the vowels. 我的C#程序假设将消息从客户端传输到服务器,但仅提取和打印元音。 But it states that my 'i' doesn't exist please help. 但是它声明我的“ i”不存在,请帮助。

// 5. Display the received message:
            Console.WriteLine("[Server] Message of client recieved");
            for (int i = 0; i < totalBytes; i++)
            {
                aChar = Convert.ToChar(incomingDataBuffer[i]);
                Console.Write(aChar);
            }

            {
               aChar = Convert.ToChar(incomingDataBuffer[i]);
               if(aChar == 'a' || aChar == 'A' || aChar == 'e' || aChar == 'E' || aChar == 'i' || aChar == 'I' || aChar == 'o' || aChar == 'O' || aChar == 'u' || aChar == 'U')
               {  
                   Console.Write(aChar);
               }

} }

You closed the for loop with the first }. 您使用第一个}关闭了for循环。 The scope of 'i' is limited to the for { ... } body, so the lower Convert.ToChar is out of the scope of the 'i', it no longer exists. 'i'的范围限于{...}主体,因此较低的Convert.ToChar不在'i'的范围内,它不再存在。

for (int i = 0; i < totalBytes; i++)
{
  aChar = Convert.ToChar(incomingDataBuffer[i]);
  Console.Write(aChar);
  aChar = Convert.ToChar(incomingDataBuffer[i]);
  if(aChar == 'a' || aChar == 'A' || aChar == 'e' || aChar == 'E' || aChar == 'i' || aChar == 'I' || aChar == 'o' || aChar == 'O' || aChar == 'u' || aChar == 'U')
  {  
    Console.Write(aChar);
  }
}

Would fix it, or you could create two for loops to get the output to be what you want it to be. 将其修复,或者您可以创建两个for循环以使输出成为您想要的样子。

您只能在for循环内使用“ i”,因为它是局部变量。

Seems like you meant to include the second part of your code in the for loop, so just need to ensure the closing curly brace is after the second part. 似乎您打算将代码的第二部分包括在for循环中,因此只需确保右花括号在第二部分之后即可。

But you can write your code in a simpler way 但是您可以以更简单的方式编写代码

 var s = Encoding.ASCII.GetString(incomingDataBuffer);
 Console.WriteLine(s);
 Console.WriteLine(string.Join("",s.Where(c => "aeiou".Contains(char.ToLower(c)))));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C# 错误:CS0103 当前上下文中不存在名称“ ” - C# error : CS0103 The name ' ' does not exist in the current context C#错误CS0103当前上下文中不存在名称“ originalImage” - C# Error CS0103 The name 'originalImage' does not exist in the current context 错误 CS0103:当前上下文中不存在名称“_context” - Error CS0103: The name '_context' does not exist in the current context 错误 CS0103:当前上下文中不存在名称“currentScreen”(CS0103) - Error CS0103: The name 'currentScreen' does not exist in the current context (CS0103) 错误CS0103:名称“ TimeSpan”在当前上下文(CS0103)(testingProgram)中不存在? - Error CS0103: The name 'TimeSpan' does not exist in the current context (CS0103) (testingProgram)? 错误cs0103名称&#39;IEnumerator&#39;在当前上下文中不存在 - error cs0103 The Name 'IEnumerator' does not exist in the current context 错误CS0103:名称“ HttpUtility”在当前上下文中不存在 - error CS0103: The name `HttpUtility' does not exist in the current context "错误 CS0103:当前上下文中不存在名称“AssetPreview”" - error CS0103: The name 'AssetPreview' does not exist in the current context 错误CS0103当前上下文中不存在名称“图像” - Error CS0103 The name 'Image' does not exist in the current context Unity错误CS0103:当前上下文中不存在名称`&#39; - Unity error CS0103: The name `' does not exist in the current context
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM