【问题标题】:Java BufferedReader.readLine() Skipped One Line [duplicate]Java BufferedReader.readLine() 跳过了一行[重复]
【发布时间】:2021-12-31 17:10:12
【问题描述】:

我正在尝试制作一些分布式 Java RMI 程序,当我需要从用户那里获取输入时遇到了问题。在下面的代码sn-p中,输入1似乎很好,但是第一次输入2没有任何打印,当我再次输入2时,它就可以了。

这是怎么发生的?检查了BufferedReader.readLine() 函数文档,但没有发现任何与此相关的内容。这跟else if有关系吗?

import java.io.*;
public class test {

    private static BufferedReader lineOfText = new BufferedReader(new InputStreamReader(System.in));

    public test() throws NumberFormatException, IOException {
        System.out.println("1 to create an auction, 2 to close an auction");
        if (Integer.parseInt(lineOfText.readLine()) == 1) {
            System.out.println("1 recieved");
        } else if (Integer.parseInt(lineOfText.readLine()) == 2) {
            System.out.println("2 recieved");
        } else {
            return;
        }
    }
    public static void main(String[] args) throws NumberFormatException, IOException {
        test t = new test();
    }
}

输入1:

1 to create an auction, 2 to close an auction
1 //<- that's the input
1 recieved

输入2:

1 to create an auction, 2 to close an auction
2 //<- that's the input yet nothing happened
2 //<- input again
2 recieved

还尝试了一些在线编译器上的代码sn-p,结果和我的VSCode一样。这可能是一个简单的问题,但我不知道发生了什么;\

【问题讨论】:

  • if (readline == 1) { ... } else if (readline == 2) {...},问题是,您的第一个 if 语句正在停止从缓冲区读取输入,而 21 不匹配,因此它下降到 else if 哪个再次等待两者的输入。做一个 readline 然后检查结果。为简单起见,我还考虑使用Scanner
  • 是的,我找到了问题所在。之前使用Scanner,但有点搞砸了,所以尝试了一些BufferedReader

标签: java user-input bufferedreader readline


【解决方案1】:

因为您在“if”和“else-if”中都调用了“readline()”,所以每次尝试检查条件时都会调用 readline() 两次:运行“if”时调用一次条件,然后在运行 else-if 时第二次。当您在条件语句中放置一个函数时,例如:

if(myFunction() == true){}

在将返回值与条件进行比较之前,它总是会运行该函数。所以当你有:

if (Integer.parseInt(lineOfText.readLine()) == 1) {
        System.out.println("1 recieved");
    } else if (Integer.parseInt(lineOfText.readLine()) == 2) {
        System.out.println("2 recieved");
    } else {
        return;
    }

它会在检查“if”时调用 readLine(),并在“else-if”时再次调用。

您应该在开始根据条件检查它之前调用 readline 以确保它只运行一次:

import java.io.*;
public class test {

private static BufferedReader lineOfText = new BufferedReader(new InputStreamReader(System.in));

public test() throws NumberFormatException, IOException {
    
    System.out.println("1 to create an auction, 2 to close an auction");
    String myLine = lineOfText.readLine(); //call readLine() before checking conditions so it only runs once
    if (Integer.parseInt(myLine) == 1) {
        System.out.println("1 recieved");
    } else if (Integer.parseInt(myLine) == 2) {
        System.out.println("2 recieved");
    } else {
        return;
    }
}
public static void main(String[] args) throws NumberFormatException, IOException {
    test t = new test();
}

}

【讨论】:

    【解决方案2】:

    当你输入2时,第一个if语句读取的数字当然不等于1,那么下一个if语句不会为你提供最后一个输入2

    您需要将readLine() 放在 if 之外,使其只读取一次。

    public class test {
    
        private static BufferedReader lineOfText = new BufferedReader(new InputStreamReader(System.in));
    
        public test() throws NumberFormatException, IOException {
            System.out.println("1 to create an auction, 2 to close an auction");
            
            int choice = Integer.parseInt(lineOfText.readLine());
            
            if (choice == 1) {
                System.out.println("1 recieved");
            } else if (choice == 2) {
                System.out.println("2 recieved");
            } else {
                return;
            }
        }
        public static void main(String[] args) throws NumberFormatException, IOException {
            test t = new test();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-23
      • 2011-08-23
      • 1970-01-01
      • 1970-01-01
      • 2017-06-03
      相关资源
      最近更新 更多