Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.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 循环内变量的初始化_Java - Fatal编程技术网

Java 循环内变量的初始化

Java 循环内变量的初始化,java,Java,上面的以下代码完全正确,并且正在执行其预期的操作。(要读取字符直到按下fullstop,然后告诉用户输入了多少空格)。虽然如果我在do while循环外初始化变量,如以下代码所示: class Spaces { public static void main(String args[]) throws java.io.IOException{ char ch; int space = 0; do { ch = (char) System.in.rea

上面的以下代码完全正确,并且正在执行其预期的操作。(要读取字符直到按下fullstop,然后告诉用户输入了多少空格)。虽然如果我在do while循环外初始化变量,如以下代码所示:

class Spaces {
public static void main(String args[])
throws java.io.IOException{

    char ch;
    int space = 0;

    do {

        ch = (char) System.in.read();
        if (ch == ' ') space++;

    } while (ch != '.'); //Stop if fullstop is provided

System.out.println("No. of spaces: " + space);
}
程序拒绝停止。当我进入句号时,程序没有停止。我知道在循环内声明一个变量意味着它在循环外的其他地方不可用。但是在循环外声明的变量(如在第二组代码中)应该在循环中和其他地方工作。所以我不明白为什么第二组代码无效(程序运行时,没有编译错误,但当输入句号时,它会继续运行)


我也明白,与这里被问到的问题相比,这是一个非常基本的问题。但我真的找不到答案。提前谢谢。

您不能使用ch=(char)System.in.read()跳出循环。因为你总是要让系统给你一个新角色


试着打印你的角色,你会发现你总是打印第一个角色,你处于无限循环中

您不能使用ch=(char)System.in.read()跳出循环。因为你总是要让系统给你一个新角色


试着打印你的角色,你会发现你总是打印第一个角色,你处于无限循环中

您不能使用ch=(char)System.in.read()跳出循环。因为你总是要让系统给你一个新角色


试着打印你的角色,你会发现你总是打印第一个角色,你处于无限循环中

您不能使用ch=(char)System.in.read()跳出循环。因为你总是要让系统给你一个新角色


试着打印你的角色,你会发现你总是打印第一个角色,你处于无限循环中

当循环开始时,
ch
中的任何值都将永远保留在那里,因为没有写入任何内容:

class Spaces {
public static void main(String args[])
throws java.io.IOException{

    char ch = (char) System.in.read();
    int space = 0;

    do {

        if (ch == ' ') space++;

    } while (ch != '.'); //Stop if fullstop is provided

System.out.println("No. of spaces: " + space);
}

当循环开始时,
ch
中的任何值都将永远保留在那里,因为没有写入任何内容:

class Spaces {
public static void main(String args[])
throws java.io.IOException{

    char ch = (char) System.in.read();
    int space = 0;

    do {

        if (ch == ' ') space++;

    } while (ch != '.'); //Stop if fullstop is provided

System.out.println("No. of spaces: " + space);
}

当循环开始时,
ch
中的任何值都将永远保留在那里,因为没有写入任何内容:

class Spaces {
public static void main(String args[])
throws java.io.IOException{

    char ch = (char) System.in.read();
    int space = 0;

    do {

        if (ch == ' ') space++;

    } while (ch != '.'); //Stop if fullstop is provided

System.out.println("No. of spaces: " + space);
}

当循环开始时,
ch
中的任何值都将永远保留在那里,因为没有写入任何内容:

class Spaces {
public static void main(String args[])
throws java.io.IOException{

    char ch = (char) System.in.read();
    int space = 0;

    do {

        if (ch == ' ') space++;

    } while (ch != '.'); //Stop if fullstop is provided

System.out.println("No. of spaces: " + space);
}

第二个程序不工作,因为您只接受了一次输入,并且由于
ch!='的值始终为真,循环从不停止。相反,您应该将
系统.in.read()
放在循环中,这样在每次迭代中,程序都会停止以获取用户输入,将其与条件进行比较,然后决定是否应执行下一次迭代。

第二个程序不工作,因为您只获取一次输入,由于
ch!='的值始终为真,循环从不停止。相反,您应该将
系统.in.read()
放在循环中,这样在每次迭代中,程序都会停止以获取用户输入,将其与条件进行比较,然后决定是否应执行下一次迭代。

第二个程序不工作,因为您只获取一次输入,由于
ch!='的值始终为真,循环从不停止。相反,您应该将
系统.in.read()
放在循环中,这样在每次迭代中,程序都会停止以获取用户输入,将其与条件进行比较,然后决定是否应执行下一次迭代。

第二个程序不工作,因为您只获取一次输入,由于
ch!='的值始终为真,循环从不停止。相反,您应该将
System.in.read()
放在循环中,这样在每次迭代中,程序都会停止进行用户输入,将其与条件进行比较,然后决定是否执行下一次迭代。

您必须从循环中调用System.in.read()。像这样:

    do {

        if (ch == ' ') space++;

    } while (ch != '.'); //Stop if fullstop is provided
按照你的方式,只有一个字符被读入


另外,我知道您不必为main方法编写throws语句。

您必须从循环内部调用System.in.read()。像这样:

    do {

        if (ch == ' ') space++;

    } while (ch != '.'); //Stop if fullstop is provided
按照你的方式,只有一个字符被读入


另外,我知道您不必为main方法编写throws语句。

您必须从循环内部调用System.in.read()。像这样:

    do {

        if (ch == ' ') space++;

    } while (ch != '.'); //Stop if fullstop is provided
按照你的方式,只有一个字符被读入


另外,我知道您不必为main方法编写throws语句。

您必须从循环内部调用System.in.read()。像这样:

    do {

        if (ch == ' ') space++;

    } while (ch != '.'); //Stop if fullstop is provided
按照你的方式,只有一个字符被读入


好了,我知道您不必为main方法编写throws语句。

您正在调用
System.in.read()
之外的
do…while
循环,因此它的值永远不会更新。输入句号不会对第二个代码产生影响,因为ch的值永远不会更新。它始终等于您将其初始化为的值(在第二个示例中,
System.in.read()

关于您的混淆,在循环外部声明的变量可以在循环中工作,但如果您想保持更改,则需要更新它。例如:

class Spaces {
public static void main(String args[])
throws java.io.IOException{

    char ch = 0;
    int space = 0;

    do {
        ch = (char) System.in.read();
        if (ch == ' ') space++;

    } while (ch != '.'); //Stop if fullstop is provided

    System.out.println("No. of spaces: " + space);
}
ch已更新。您还可以

    char ch; 
    int space = 0;

    do {

        ch = (char) System.in.read(); //ch is updated
        if (ch == ' ') space++;

    } while (ch != '.'); 

实际在循环外部实例化变量,但同样,值必须在循环内部更新。

您在
do…while
循环外部调用
System.in.read()
int a = (int) System.in.read();
int b = a;
System.out.println("a is: " + a);
System.out.println("b is: " + b);
int a = (int) System.in.read();
System.out.println("a is: " + a);
a = (int) System.in.read();
System.out.println("a is: " + a);
// ch is declared and initialised
char ch = (char) System.in.read();
int space = 0;

// There are no assignments to ch in the loop, so it remains invariant 
// during the loop's execution. This means if `ch != '.'` tests true
// once it will continue testing true.
do {
    if (ch == ' ') space++;
} while (ch != '.');
// ch is declared and initialised
char ch;
int space = 0;

// ch is assigned during the loop to an impure expression and has the 
// potential to vary for each iteration of the loop. This means even
// if `ch != '.'` tests true once it may not test true next iteration.
do {
    ch = (char) System.in.read();
    if (ch == ' ') space++;
} while (ch != '.');