Java “生成另一个”的问题;笑话;陈述

Java “生成另一个”的问题;笑话;陈述,java,arrays,arraylist,netbeans,Java,Arrays,Arraylist,Netbeans,目前,我正在创建一个向用户讲述笑话的简单程序 一美元怎么能买到四套西装?键入“显示”以查看答案 你:揭示 机器人:买一副牌。咯咯地笑 机器人:你想继续吗?是还是否? 你:是的 机器人:你怎么说回飞棒不会回来了?键入“显示”以查看答案 那是我预定的剧本。然而,我面临一个问题: 机器人:你想继续吗?是还是否? 你:是的 机器人:错误>:D 机器人打印出“错误”,而不是生成另一个笑话。这是我的密码: public class NewClass { public static void mai

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

一美元怎么能买到四套西装?键入“显示”以查看答案
你:揭示
机器人:买一副牌。咯咯地笑
机器人:你想继续吗?是还是否?
你:是的
机器人:你怎么说回飞棒不会回来了?键入“显示”以查看答案

那是我预定的剧本。然而,我面临一个问题:

机器人:你想继续吗?是还是否?
你:是的
机器人:错误>: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");
}

实际上,您不会在循环中提示用户进行进一步的输入,因此用户无法实际回答问题,也没有一种机制可以让用户实际讲更多的笑话。所有这些逻辑都完全在循环之外,因此它不会再次发生。

您的
Do-While循环不正确,我建议您执行以下操作:

userInput = input.nextLine();
userInput = userInput.toLowerCase();

if (userInput.equals("reveal")) {
    System.out.println("Bot: " + jokesAnswer[randomNumber]);
} else {
    System.out.println("Bot: Wrong! >:D");
}

System.out.println("Bot: Do you want to hear another joke? :D Yes or No?");
请注意,bot会等待您编写“discover”,没有它就无法继续,但我相信您看到了要点,可以根据需要修改代码。(如有疑问,请随时询问)

如果您不想在
userInput!=“显示”
并询问是否要继续,您可以将内部的
do while循环
替换为以下内容:

userInput = input.nextLine();
userInput = userInput.toLowerCase();

if (userInput.equals("reveal")) {
    System.out.println("Bot: " + jokesAnswer[randomNumber]);
} else {
    System.out.println("Bot: Wrong! >:D");
}

System.out.println("Bot: Do you want to hear another joke? :D Yes or No?");

在这种情况下,无论机器人是否泄露笑话或说“错”,它都会询问您是否继续。

您的逻辑有点不正确。你需要在do-while循环中讲笑话,否则你只能讲一次。您需要能够处理这样的情况,即他们重复键入除“揭示”以外的内容,因此我为该情况添加了另一个循环。这是一个while循环,而不是do-while循环,因为根本不需要保证它会发生

Scanner input = new Scanner(System.in);

do {
    randomNumber = (int) (Math.random() * jokes.length);
    System.out.println("Bot: " + jokes[randomNumber] + " Type reveal to view answer");

    System.out.print("You: ");
    userInput = input.nextLine();
    userInput = userInput.toLowerCase();

    while (!userInput.equals("reveal"))
    {
        System.out.println("Bot: Wrong! >:D");

        System.out.print("You: ");
        userInput = input.nextLine();
        userInput = userInput.toLowerCase();
    }

    System.out.println("Bot: " + jokesAnswer[randomNumber] + "\nBot: Do you want to hear another joke? :D Yes or No?");

    System.out.print("You: ");
    userInput = input.nextLine();
    userInput = userInput.toLowerCase();

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

我还会将用户输入语句移动到它们自己的函数中,以避免代码重复。

您的逻辑有点混乱。userInput仅在do循环内部并在那里输出时分配。因此,即使键入“否”,它也会首先“出错”。请手动逐步检查代码。你会得到同样的结果。一个有用且常见的结构是
x=someMethod();当(x!=y){process(x);x=someMethod();}
Debug通过你的代码,你就会意识到哪里出了问题。在这样简单的代码流中,大多数逻辑错误都可以通过简单的代码逻辑干运行来发现。就像这里,当
userInput
no
时,这不是
显示的
因此输出错误。你可能还想使用
equalsIgnoreCase
而不是仅仅使用
equals
@4castle my bad:(哦,好吧,我对扫描仪不熟悉,但我要编辑答案。你这么做了。好的,我正在删除我的评论。