产生另一个问题"笑话"语句

时间:2017-07-25 14:51:00

标签: java arrays arraylist netbeans

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

  

Bot:你怎么能得到一套1美元的四件套装?键入显示查看答案
  你:透露
  Bot:买一副纸牌。 咯咯咯咯的笑声
  Bot:你想继续吗?是或否?
  你:是的   Bot:你怎么称待不会回来的回旋镖?键入显示以查看答案

那是我想要的剧本。但是,我遇到了一个问题:

   Bot:你想继续吗?是或否?
  你:是的   Bot:错误>: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"));

    }

4 个答案:

答案 0 :(得分:1)

您的代码失败的原因是您的逻辑没有通过用户输入获取所有可能的场景:您在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");
}

答案 1 :(得分:0)

你实际上没有提示用户在循环内进一步输入,所以用户无法真正回答问题,也没有机制让它真正告诉你进一步的笑话。所有这些逻辑都完全在循环之外,所以它不会再发生。

答案 2 :(得分:0)

您的Do-While-Loop不正确,我建议您使用以下内容:

请注意,机器人等待你写“揭示”,如果没有它就不能继续,但我确定你看到了这一点并且可以根据需要改变代码。 (如果不随意问)

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?"};

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

        do{            
            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("reveal"))

        userInput = input.nextLine();
        userInput = userInput.toLowerCase();
    }while(!userInput.equals("no"))
}

如果您想在userInput != "reveal"之后不想揭开笑话,只想询问是否继续,则可以使用以下内容替换内部do-while-loop

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?");

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

答案 3 :(得分:0)

你的逻辑有点破碎。你需要在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"));

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