使用 SCANNER 读取用户的输入行



我有一个程序需要读取输入行。它需要一次是多行。例如:


当我进入我的时间机器或也许不是,我想知道自由意志是否存在?我想知道自由意志是否存在也许不是当我进入我的时间机器或。


所有这些都由用户一次输入。我试图使用扫描仪类中的 .hasNextLine() 方法,但它没有返回 false....它再次等待输入。我一直在寻找解决方案,似乎 .hasNextLine() 等待输入,但我不知道使用什么替代方案。有什么建议吗?实际代码如下所示:

while(input.hasNextLine());
        {
            line += input.nextLine();
        }

感谢您的帮助

也许你应该使用某种"停止"序列,意思是当用户输入特定的字符序列时,它会打破循环。它可能看起来像这样:

public static void main(String args[]){
    final String stopSequence = "/stop";
    final Scanner reader = new Scanner(System.in);
    String input = reader.nextLine();
    while(!input.equalsIgnoreCase(stopSequence)){
        //process input
        input = reader.nextLine();
    }
}

相关内容

最新更新