我无法初始化for循环中使用的变量

问题描述 投票:0回答:1

screenshot in rider

for (int x = 5; x < 10; x++)
{
    Console.WriteLine(x);
    
}

Console.WriteLine(x);// A local or parameter named 'x' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter

int x = 10;// Cannot use local variable 'x' before it is declared

我问了 CHAT GPT 但它没有帮助我(我使用了很多不同的方法来解决这个问题)

c# for-loop initialization
1个回答
0
投票

你是否正在尝试做这样的事情:

int x = 5;

for (; x < 10; x++)
{
    Console.WriteLine(x);
    
}

Console.WriteLine(x);
© www.soinside.com 2019 - 2024. All rights reserved.