我正在制作一个可以与你进行对话的人工智能。 当你运行程序时,计算机会说“嗨”,用户可以输入多个问候语(例如“你”、“你好”等)
我的问题是当计算机询问用户“你好吗?”时和用户回答。我编写了一个开关,如果你说“好”之类的话,计算机就会回复“很高兴听到它”。 ,但事实并非如此。我做错了什么?
这是我的代码:
System.out.println("hi");
Thread.sleep(3000);
System.out.println("The computer would like to remind you to reply with only a greeting");
Scanner rexy = new Scanner(System.in);
String domino = rexy.nextLine();
switch (domino){
case "hello":
System.out.println("How are you?");
break;
case "hi":
System.out.println("How are you?");
break;
case "howdy":
System.out.println("How are you?");
break;
case "heyo":
System.out.println("How are you?");
break;
case "hello?":
System.out.println("How are you?");
break;
case "hey":
System.out.println("How are you?");
break;
case "sup":
System.out.println("How are you?");
break;
case "good":
System.out.println("Glad to hear it");
break;
case "great":
System.out.println("Glad to hear it");
break;
case "awesome":
System.out.println("Glad to hear it");
break;
case "splendid":
System.out.println("Glad to hear it");
break;
case "fantastic":
System.out.println("Glad to hear it");
break;
case "fine":
System.out.println("Glad to hear it");
break;
case "what's crackalakin?":
System.out.println("How are you?");
break;
case "what's up turd face?":
System.out.println("That's rude! How are you?");
break;
}
}
}
谢谢。
最佳答案
您可以尝试在开关中添加一个default
语句,以便在无法识别答案时有后备;像这样:
switch (domino) {
//...
default:
System.out.println("Sorry, I don't understand that.");
break;
}
此外,您可以尝试打印 domino
字符串,以查看实际读取的内容。
System.out.println(domino);
另外,还有一个提示:您可以在 switch
中连接多个相等的 case
语句,如下所示:
switch (domino) {
case "hello":
case "hi":
case "howdy":
case "heyo":
case "hello?":
case "hey":
case "sup":
System.out.println("How are you?");
break;
case "good":
case "great":
case "awesome":
case "splendid":
case "fantastic":
case "fine":
System.out.println("Glad to hear it");
break;
}
关于java - java中允许多个用户输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25372949/