Java 在for循环中声明的变量的范围

Java 在for循环中声明的变量的范围,java,scope,Java,Scope,让我解释一下疑问 我在main方法中有一个inti=9。现在我在同一个main方法中有了for循环 class Test { public static void main(String... args) throws Exception{ int i =39; for (int i = 0; i < 10; i++) { // error:i already defined } } } class Test { public stat

让我解释一下疑问

我在main方法中有一个
inti=9
。现在我在同一个main方法中有了for循环

class Test {

  public static void main(String... args) throws Exception{   
    int i =39;
    for (int i = 0; i < 10; i++) {   // error:i already defined
    }
  }

}
class Test {

  public static void main(String... args) throws Exception{   

    for (int i = 0; i < 10; i++) {
    }
    System.out.println(i); // error: cannot find symbol i
  }

}
类测试{
公共静态void main(字符串…args)引发异常{
int i=39;
对于(int i=0;i<10;i++){//错误:我已定义
}
}
}
在上面的例子中,它显示了已经定义了i的编译时错误。从这个错误中,我认为在For条件中声明的i的范围也在循环之外

现在看下面的例子

class Test {

  public static void main(String... args) throws Exception{   
    int i =39;
    for (int i = 0; i < 10; i++) {   // error:i already defined
    }
  }

}
class Test {

  public static void main(String... args) throws Exception{   

    for (int i = 0; i < 10; i++) {
    }
    System.out.println(i); // error: cannot find symbol i
  }

}
类测试{
公共静态void main(字符串…args)引发异常{
对于(int i=0;i<10;i++){
}
System.out.println(i);//错误:找不到符号i
}
}
在上面的示例中,它显示了找不到符号i的循环外部的错误

如果它没有找到在for循环条件中声明的i。那么它为什么显示i在第一个示例中已经根据

块是平衡大括号之间的一组零或多个语句,可以在允许使用单个语句的任何地方使用

所以

无论在块内声明了什么变量,作用域都限制在该块内

拇指规则将显示变量的作用域在
{}

public static void main(String... args) throws Exception{   
    int i =39;
    for (int i = 0; i < 10; i++) {   
     System.out.println(i); // which i ?? 
    }
  }

在上述情况下,
i
范围已在{}的
中结束,外部不可用。

在第一个场景中,
i
具有方法范围。它在声明之后的
main()
方法中随处可见。因此,当您试图在
for
中声明另一个
i
时,它会给出已定义的错误

  public static void main(String... args) throws Exception{   
    int i =39; // i will be visible throughout the main method
    for (int i = 0; i < 10; i++) {   // i already declared above, thus the error
    }
  }
第一个例子:
i
范围是完整的
main
方法

第二个例子:
i
范围是
for
循环

干杯

你能想到的

for (int i = 0; i < 10; i++) {
}
下面的代码给出了错误

int i =39;
for (int i = 0; i < 10; i++) {   // error:i already defined
}
inti=39;
对于(int i=0;i<10;i++){//错误:我已定义
}
下面不会给出错误

for (int i = 0; i < 10; i++) {   
}
int i =39;
for(inti=0;i<10;i++){
}
int i=39;
在本例中

class Test {

  public static void main(String... args) throws Exception{   
    int i =39;
    for (int i = 0; i < 10; i++) {   // error:i already defined
    }
  }

}
class Test {

  public static void main(String... args) throws Exception{   

    for (int i = 0; i < 10; i++) {
    }
    System.out.println(i); // error: cannot find symbol i
  }

}
i
的范围在for循环中,您试图在for循环外打印
i

这是工作代码

class Test {

  public static void main(String... args) throws Exception{   
    int i;
        for ( i = 0; i < 10; i++) {
        }
        System.out.println(i); // error: cannot find symbol i
      }

}
类测试{
公共静态void main(字符串…args)引发异常{
int i;
对于(i=0;i<10;i++){
}
System.out.println(i);//错误:找不到符号i
}
}

输出10

您所要做的就是在for循环之前添加另一个变量。然后,和你们需要的i核对一下

public static void main(String[] args) {
    int j = 0;
    for (int i = 0; i < 10; i++) {
        j = i;
    }
    System.out.println(j);
}
publicstaticvoidmain(字符串[]args){
int j=0;
对于(int i=0;i<10;i++){
j=i;
}
系统输出println(j);
}
class Test {

  public static void main(String... args) throws Exception{   

    for (int i = 0; i < 10; i++) {
    }
    System.out.println(i); // error: cannot find symbol i
  }

}
class Test {

  public static void main(String... args) throws Exception{   
    int i;
        for ( i = 0; i < 10; i++) {
        }
        System.out.println(i); // error: cannot find symbol i
      }

}
public static void main(String[] args) {
    int j = 0;
    for (int i = 0; i < 10; i++) {
        j = i;
    }
    System.out.println(j);
}