我正在尝试为我的频道制作一个IRC机器人。我希望该机器人能够从控制台接收命令。为了让主循环等待用户输入,我添加了以下循环:
while(!userInput.hasNext());
这似乎没有起作用。我听说过BufferedReader,但从未使用过,也不确定它是否能够解决我的问题。
while(true) {
System.out.println("Ready for a new command sir.");
Scanner userInput = new Scanner(System.in);
while(!userInput.hasNext());
String input = "";
if (userInput.hasNext()) input = userInput.nextLine();
System.out.println("input is '" + input + "'");
if (!input.equals("")) {
//main code
}
userInput.close();
Thread.sleep(1000);
}