如何在一个或多个for循环中使用现有变量?

我正在通过Head First C#工作,我对目前的练习感到有些困惑。他们说: 如果你在for循环中声明一个变量 - for(int c = 0; ...) - 那么该变量只在循环的花括号内有效。因此,如果你有两个使用变量的for循环,你要么在每个循环中声明它,要么在循环外有一个声明。如果变量c已经在循环之外声明,则不能在任何一个中使用它。 这听起来与我相矛盾,几乎就像说你只能在外面宣布它,但如果你在外面宣布它就不能使用它。 你也可以,或者不是吗?我尝试在两个单独的for循环中声明c并且它工作正常,但是当在for循环之外声明c时,我找不到任何方法来引用两个for循环中的变量c同时它也在外面声明,无论我是否尝试改变它的价值与否。这不是练习所必需的,我只是试图吸收我遇到的每一点知识并试图超越材料。 这本书可能让我感到困惑,所以如果这是不可能的并且完全没必要,请告诉我,谢谢!     
已邀请:
问题是范围界定。请阅读此处,了解有关变量作用域如何在C#中工作的一些细节。 如果在循环外声明变量,则无法在内部重新声明: 坏:
int c = 0;
for(int c = 0; c < list.Count; c++) // Error!
{

}
好: 在外面声明,用于内部:
int c = 0;
for(c = 0; c < list1.Count; c++)
{
}

for(c = 0; c < list2.Count; c++)
{
}
在两个循环中声明:
for(int c = 0; c < list1.Count; c++)
{
}

for(int c = 0; c < list2.Count; c++)
{
}
    
你可以这样做
  int i;
  for (i = 0; i < 3; i++)
    foo(i);
  for (i = 0; i < 5; i++)
    bar(i);
要么
 for (int i = 0; i < 3; i++)
    foo(i);
 for (int i = 0; i < 5; i++)
    bar(i);
但不是
int i;
for (int i = 0; i < 3; i++) //error
  foo(i);
for (int i = 0; i < 5; i++)
  bar(i);
    
该陈述确实令人困惑,如果我理解正确,根据案文我不应该这样做:
int i;
for (i = 1; i < 10; i++) { }

for (i = 0; i < 20; i++) { }
但我可以,这显然是有效的。我认为文本的意思是“你不能在任何一个中重新声明它”而不是“你不能在任何一个中使用它”。     
我认为他们的意思如下。 你可以这样做。听到的是你已经在循环之外声明了一个变量并且正在使用它。但问题是您可能会覆盖需要在其他地方使用的现有值。
int i = 0;

for (i = 0; i < 100; i++)
{
    // Do something
}
你真的不能做的就是这个。在这里,您将重新使用内部
for
外部
for
的变量。
for (int i = 0; i < 100; i++)
{
    for (i = 0; i < 100; i++)
    {
        // Do something
    }
}
    
这里的概念是范围。变量在某个范围内声明,不能在其外部访问。这有助于定义变量的生命周期以及控制对变量的访问。变量可以在方法中的类,方法或条件范围内声明,例如在if语句或for循环中。 考虑范围的一种简单方法是您可以访问变量  在这对花括号内,它生活在。 这是一个例子:
    public class TestClass
    {
        int p;  // p's is in the 'TestClass' scope

        public void TestFunction1()
        {

            Console.WriteLine(p);   // OK, p in class scope

            //  a lives in the 'TestFunction' scope
            int a = 1; // Declared outside of any loop.

            for (int i = 0; i < 10; i++)
            {
                //  i lives in the scope of this for loop
                Console.WriteLine(i);
                //  a is still accessible since this for loop is inside TestFunction1
                Console.WriteLine(a);
            }
            Console.WriteLine(a); // OK, in scope
            //Console.WriteLine(i); // Error, i out of scope

            //  j also lives in the TestFunction1 scope
            int j = 0;
            for (j = 0; j < 20; j++)
            {
                //  j still accessible within the loop since the loop is within TestFunction1
                Console.WriteLine(j);
            }

            Console.WriteLine(j); // Ok, j still in scope (outside of loop)
        }

        public void TestFunction2()
        {
            Console.WriteLine(p);   // Ok, TestFunction2 is in the TestClass scope like TestFunction1
            //Console.WriteLine(a);   // Error, a lives in TestFunction1
            //Console.WriteLine(i);   // Error, allright now we're just getting silly ; )
        }
    }
    
您可以使用现有变量作为for循环的起点。 我刚开始学习C#4周前,所以要疲倦..但语法是:
int x = 10;

for (x = x ; x < 15; x++) // x starts off as whatever defined above (15)
    {
        Console.WriteLine("x is: {0}", x);
    }
    // After for is done executing, x will = 15
然后无论出于何种原因,你可以继续做其他逻辑 (当X <30时,发生其他事情) EX)
for (x = x ; x < 30; x++)
    {
        // Do stuff here
    }
    
x = x用于重用变量,将其值从一个循环保持到另一个循环,但它会给出警告。 你可能更喜欢这种语法
for (x+=0; x>10; x++) ;
    

要回复问题请先登录注册