gpt4 book ai didi

java - 生成另一个 "Joke"语句时出现问题

转载 作者:行者123 更新时间:2023-11-30 06:35:15 24 4
gpt4 key购买 nike

目前,我正在创建一个简单的程序,向用户讲笑话。

Bot: How can you get four suits for a dollar? Type reveal to view answer
You: reveal
Bot: Buy a deck of cards. giggle giggle
Bot: Do you wish to continue? Yes or no?
You: Yes
Bot: What do you call a boomerang that won't come back? Type reveal to view answer

那是我想要的脚本。然而,我遇到了一个问题:

Bot: Do you wish to continue? Yes or no?
You: Yes
Bot: Wrong >:D

机器人打印出“错误”,而不是生成另一个笑话。这是我的代码:

public class NewClass {

public static void main(String[] args) {
int randomNumber;
String userInput;

String[] jokesAnswer = {"Snowballs! HAHA ", "A stick!!! :D", "A pork chop.", "A piano! HEHE ", "A dead centipede!! xD", "At the crystal ball. *chuckle chuckle*",
"A conversation. TEEHEE", "Buy a deck of cards. *giggle giggle*"};
String[] jokes = {"What is the difference between a snowman and a snowwoman?", "What do you call a boomerang that won't come back?",
"What do you call a pig that does karate?", "What has a lot of keys but can not open any doors?", "What lies on its back, one hundred feet in the air?",
"Where do fortune tellers dance?", "What can you hold without ever touching it?", "How can you get four suits for a dollar?"};

randomNumber = (int) (Math.random() * jokes.length);
System.out.println("Bot: " + jokes[randomNumber] + " Type reveal to view answer");
Scanner input = new Scanner(System.in);
do {
System.out.print("You: ");
userInput = input.nextLine();
userInput = userInput.toLowerCase();
if (userInput.equals("reveal")) {
System.out.println("Bot: " + jokesAnswer[randomNumber] + "\nBot: Do you want to hear another joke? :D Yes or No?");
} else {
System.out.println("Bot: Wrong! >:D");
}
} while (!userInput.equals("no"));

}

最佳答案

代码失败的原因是您的逻辑没有通过用户输入来处理所有可能的场景:您处于 while 循环中,直到失败

(!userInput.equals("no"));

但在循环内你有这个

if (userInput.equals("reveal")) {
System.out.println(
"Bot: " + jokesAnswer[randomNumber] + "\nBot: Do you want to hear another joke? :D Yes or No?");
} else {
System.out.println("Bot: Wrong! >:D");
}

所以基本上任何不“揭示”的东西都会让你达到同样的目的......

解决方案:

您可以扩展选项

else if (userInput.equals("yes")) {
System.out.println("yes it is");
} else {
System.out.println("Bot: Wrong! >:D");
}

关于java - 生成另一个 "Joke"语句时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45306786/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com