Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 重复局部变量x_Java - Fatal编程技术网

Java 重复局部变量x

Java 重复局部变量x,java,Java,我有一个问题,for循环说它是一个重复的局部变量?我仍在努力了解这一点,我已经尽我所能研究了这一点,以便自己找到答案,所以我现在向你们大家提问。如果你不介意的话,我想解释一下为什么它不起作用 int x = 3; String name = "Dirk"; x = x * 17; System.out.print("x is " + x); double d = Math.random(); while (x > 12) { x = x - 1; } for (int x =

我有一个问题,for循环说它是一个重复的局部变量?我仍在努力了解这一点,我已经尽我所能研究了这一点,以便自己找到答案,所以我现在向你们大家提问。如果你不介意的话,我想解释一下为什么它不起作用

int x = 3;
String name = "Dirk";
x = x * 17;
System.out.print("x is " + x);
double d = Math.random();

while (x > 12) {
    x = x - 1;
}

for (int x = 0; x < 10; x = x + 1) {
    System.out.print("x is now " + x);
}

if (x == 10) {
    System.out.print("x must be 10");
} else {
    System.out.print("x isn't 10");
}
if ((x < 3) & (name.equals("Dirk"))) {
    System.out.println("Gently");
}
intx=3;
String name=“Dirk”;
x=x*17;
系统输出打印(“x为”+x);
double d=Math.random();
而(x>12){
x=x-1;
}
对于(int x=0;x<10;x=x+1){
系统输出打印(“x现在是”+x);
}
如果(x==10){
系统输出打印(“x必须为10”);
}否则{
系统输出打印(“x不是10”);
}
if((x<3)和(name.equals(“Dirk”)){
System.out.println(“轻轻地”);
}

只需在
for
循环中删除第二个声明并重新分配
x

for ( ; x < 10; x++) {
    System.out.print("x is now " + x);
}
(;x<10;x++)的
{
系统输出打印(“x现在是”+x);
}
否则,您可以简化以下说明:

x++
而不是
x=x+1


x*=19
而不是
x=x*19

您声明x两次
int x=3
第一行和for循环中的
for(int x=0;){
要解决问题,只需使用
for(x=0;。
不带类型,或者在循环中使用另一个变量名
for(int y=0;。
例如,Java中的>可能重复,同一个变量中不能有两个同名的不同局部/参数变量(重叠)范围您定义int x=3;在第一行中,然后在循环内部(int x=0;x<10;x=x+1)啊,好吧!我想最好使用的是从一开始就使用的一个,因为我希望它继续向下,并继续通过程序?我正在使用Head first Java书籍来学习我能学到的东西。谢谢!谢谢Idriss!