我正在尝试创建一个聊天机器人程序,但我的问题是,当我运行该程序时,它会在输入句子后立即退出。
import java.util.*;
public class Mina{
public static void main(String[]args){
ai();
}
public static void ai(){
greeting();
conversation();
}
public static void greeting(){
System.out.println("Hello. I am Mina.");
}
public static void conversation(){
Scanner console = new Scanner(System.in);
String chat = console.nextLine();
if(!chat.equalsIgnoreCase("\bBye\b") || !chat.equalsIgnoreCase("\bBye.\b")){
keywords(chat);
}
}
public static void keywords(String word){
if(word.equalsIgnoreCase("\bHello\b") || word.equalsIgnoreCase("\bHello.\b")){
System.out.println("What do you want to talk about?");
keywords(word);
}else if(word.equalsIgnoreCase("\bMr. Smith\b") || word.equalsIgnoreCase("\bMr. Smith.\b")){
System.out.println("I bet he is a nice teacher.");
keywords(word);
}else if(word.equalsIgnoreCase("\bBye\b") || word.equalsIgnoreCase("\bBye.\b")){
System.exit(0);
}
return;
这就是我得到的: Mina's Run
我以前通过在方法的末尾放置keyword();
来获得我的代码循环。
}else if(word.equalsIgnoreCase("\bBye\b") || word.equalsIgnoreCase("\bBye.\b")){
System.exit(0);
}
keyword(word);
return;
如果有人对我可以做些什么来解决这个问题有任何想法,请帮忙。
答案 0 :(得分:0)
我认为您需要在输入
后删除if
模块
if(!chat.equalsIgnoreCase("\bBye\b") || !chat.equalsIgnoreCase("\bBye.\b")){
keywords(chat);
}
应该是这样的
import java.util.*;
public class Mina{
public static void main(String[]args){
ai();
}
public static void ai(){
greeting();
conversation();
}
public static void greeting(){
System.out.println("Hello. I am Mina.");
}
public static void conversation(){
Scanner console = new Scanner(System.in);
String chat = console.nextLine();
keywords(chat);
}
public static void keywords(String word){
if(word.equalsIgnoreCase("\bHello\b") || word.equalsIgnoreCase("\bHello.\b")){
System.out.println("What do you want to talk about?");
keywords(word);
}else if(word.equalsIgnoreCase("\bMr. Smith\b") || word.equalsIgnoreCase("\bMr. Smith.\b")){
System.out.println("I bet he is a nice teacher.");
keywords(word);
}else if(word.equalsIgnoreCase("\bBye\b") || word.equalsIgnoreCase("\bBye.\b")){
System.exit(0);
}
return;
=============================================== ====
因此它会提示用户输入,并以输入作为参数调用函数'keywords()'。 然后转到函数定义并检查是否以递归方式满足条件
答案 1 :(得分:0)
我已经修改了你的代码,使其以一种方式工作,它会要求用户输入一个新的行,直到他们说"再见"。这可以通过使用while
循环来实现,如下所示:
import java.util.*;
public class Mina{
public static void main(String[]args){
ai();
}
public static void ai(){
greeting();
conversation();
}
public static void greeting(){
System.out.println("Hello. I am Mina.");
}
public static void conversation(){
Scanner console = new Scanner(System.in);
String chat = console.nextLine();
while(!(chat.equalsIgnoreCase("Bye") || chat.equalsIgnoreCase("Bye."))){
keywords(chat);
chat = console.nextLine();
}
System.exit(0);
}
public static void keywords(String word){
if(word.equalsIgnoreCase("Hello") || word.equalsIgnoreCase("Hello.")){
System.out.println("What do you want to talk about?");
}else if(word.equalsIgnoreCase("Mr. Smith") || word.equalsIgnoreCase("Mr. Smith.")){
System.out.println("I bet he is a nice teacher.");
}
}
}