while 循环无法正常工作 java

while loop does not work properly java

在以下继续程序的代码中,用户应按 y 字符 但是当我按 y 时循环终止

public class JavaFile
{

    int i = 0;
    public void systemIO()throws java.io.IOException {
        System.out.println("Enter the character");
        i = System.in.read();
        System.out.println("the character Enter by the user : "+(char)i);
        System.out.println("the assci vlue "+i);
    }
    public void cntApp()throws java.io.IOException{
        char cnt = 'y';
        while(cnt =='y'){
            systemIO();
            System.out.println("press 'y'  if you want continue");
            cnt = (char)System.in.read(); 
            System.out.println("The Entery value   "+cnt);
        }

    }
    public static void main(String args[]) {
        try{
        new JavaFile().cntApp();
    }catch(java.io.IOException ioExe){
        System.out.println(ioExe);
    }
    }

如果你 运行 它处于调试状态:

while(cnt =='y'){
        systemIO();
        System.out.println("press 'y'  if you want continue");
        cnt = (char)System.in.read(); 
        System.out.println("The Entery value   "+cnt);
}

你会看到

cnt = (char)System.in.read();

return '\r',对应回车验证

所以,我将改用扫描仪,它会是这样的:

public class JavaFile {
    int i = 0;
    Scanner reader = new Scanner(System.in);
    public void systemIO()throws java.io.IOException {
        System.out.println("Enter the character");
        i = reader.next().charAt(0);
        System.out.println("the character Enter by the user : "+(char)i);
        System.out.println("the assci vlue "+i);
    }
    public void cntApp()throws java.io.IOException{
        char cnt = 'y';
        while(cnt =='y'){
            systemIO();
            System.out.println("press 'y'  if you want continue");
            cnt = reader.next().charAt(0);
            System.out.println("The Entery value   "+cnt);
        }

    }
    public static void main(String args[]) {
        try{
            new JavaFile().cntApp();
        }catch(java.io.IOException ioExe){
            System.out.println(ioExe);
        }
    }
}

您可以重写 cntApp 方法以使用扫描仪并获取输入的第一个字符

public void cntApp() throws java.io.IOException {
    Scanner in = new Scanner(System.in);
    char cnt = 'y';
    while (cnt == 'y') {
        systemIO();
        System.out.println("press 'y'  if you want continue");
        cnt = in.next().charAt(0);
        System.out.println("The Enter value   " + cnt);
    }
}

看起来键盘输入被缓冲了,下面的代码正在获取导致 while 循环终止的 line-terminator/null 值。

cnt = (char)System.in.read();

尝试使用 Scanner class 而不是 System.in.read()

重构代码
import java.util.Scanner;    

public class JavaFile {
        private Scanner scanner = new Scanner(System.in);
...

    public void cntApp() throws java.io.IOException {

        String line = "y";
        while ("y".equals(line)) {
            systemIO();
            System.out.println("press 'y'  if you want continue");
            line = scanner.nextLine();
            System.out.println("The Entery value   " + line);
        }

        scanner.close();
    }
...

参照this answer.

您可以在下面的行中添加 systemIO()cntApp() 方法的末尾,即在 System.in.read() 方法调用之后。

System.in.skip(System.in.available());

因此您的方法将如下所示。

public void systemIO() throws java.io.IOException {
    System.out.println("Enter the character");
    i = System.in.read();
    System.out.println("the character Enter by the user : " + (char) i);
    System.out.println("the assci vlue " + i);
    System.in.skip(System.in.available());
}

public void cntApp() throws java.io.IOException {
    char cnt = 'y';
    while (cnt == 'y') {
        systemIO();
        System.out.println("press 'y'  if you want continue");
        cnt = (char) System.in.read();
        System.out.println("The Entery value   " + cnt);
        System.in.skip(System.in.available());
    }
}

注意: 但是使用 Scanner(System.in) 比直接使用 System.in.read() 更好。