我希望程序打印输出,直到用户键入“再见”为止。但是,当我键入“ bye”时,循环/程序不会停止。方法如下:
POST /get.request.test HTTP/1.1
Origin: http://localhost:8080
Content-Length: 25
Accept-Language: zh-CN,zh;q=0.9
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Accept: application/json, text/plain, */*
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36
Host: localhost
Referer: http://localhost:8080/load
Pragma: no-cache
Cache-Control: no-cache
Content-Type: application/x-www-form-urlencoded
param%5Bid%5D=1&name=phey
答案 0 :(得分:1)
尝试while循环
while(!(userInput.equalsIgnoreCase("bye"))) {
System.out.println(chatty.getResponse(userInput));
userInput = myScanner.nextLine();
}
答案 1 :(得分:-1)
根据您的代码,如果用户在应用程序启动后立即输入"bye"
,则由于循环外的scanner.next()
行代码而无法使用。首次输入后,您的程序将正常运行。要解决此问题,只需在循环外部删除scanner.next()
条目。
public static void startConversation(ChattyBot chatty) {
Scanner myScanner = new Scanner(System.in);
String userInput;
do
{
userInput = myScanner.nextLine();
System.out.println(chatty.getResponse(userInput));
}
while (!(userInput.equalsIgnoreCase("bye")));
}