while循环不能正常工作java

时间:2015-03-04 09:12:03

标签: java loops io

在以下用于继续程序的代码中,用户应按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);
    }
    }

4 个答案:

答案 0 :(得分:1)

如果你在debug中运行它:

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);
        }
    }
}

答案 1 :(得分:1)

看起来键盘输入是缓冲的,以下代码获取行终止符/空值,导致while循环终止。

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

尝试使用Scanner类而不是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();
    }
...

答案 2 :(得分:1)

参考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()

答案 3 :(得分:0)

您可以重写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);
    }
}