2017-08-03 17 views
-9

我正在写一个聊天机器人,但似乎我不能回到起点。 我是一名初学者,所以请帮助我。我已经看到我的代码也有点混乱。我希望你可以阅读他们。感谢您的帮助!我想尽可能简单地回到这段代码的开头。谢谢

public static void main(String[] args) { 
    Scanner userinput = new Scanner(System.in); 
    Scanner choice = new Scanner(System.in); 
    String message; 
    String GenderInput; 
    //popup for 1 to chat and 2 to play 
    boolean gendercheck = false; 

我想回到这个代码

的System.out.println( “请按\” 1 \ “聊天” + “&” + “\” 2 \”打游戏“+”& \“3 \”编辑对话“);

而不是结束。在我输入结尾后,聊天机器人就会结束而不回到主屏幕。我见过使用方法来做这件事的朋友,但是因为我在这里使用了开关盒,所以我可以轻松地做到这一点吗?

System.out.println("Press \"1\" to chat" + " & " + "\"2\" to play games" + " & \"3\" to edit the conversations"); 

    int startup = choice.nextInt(); 
    String returningCode = returnChoiceOfChatbot(startup); 
    System.out.println(returningCode); 

    switch (startup) { 
     //end of main if 
     case 1: 
      //if the user wants the chatbot 
      System.out.println("Bot:\tI am Mat, a chatbot. May i know your name please?");   //asking for the user's name 
      String name = userinput.nextLine(); 
      name = name.toLowerCase(); 
      String username = name.substring(0, 1).toUpperCase() + name.substring(1); 
      message = ">>> \tHello! " + username + "! Are you a boy or a girl?";      // asking for gender 
      System.out.println(message); 
      while (gendercheck != true) { 
       GenderInput = userinput.nextLine().toLowerCase();// F 
       switch (GenderInput) { 
        case "boy": 
        case "man": 
        case "m": 
        case "male": 
        case "dude": 
        case "guy": 
         System.out.println(">>> \tHi. Mr. " + username + ". How are you?");    //printing out Mr and name if it is a male 
         gendercheck = true; 
         break; 
        case "girl": 
        case "female": 
        case "f": 
        case "woman": 
        case "lady": 
         System.out.println(">>> \tHi. " + "Ms. " + username + ". How are you?");  //printing out Ms and name if it is a female 
         gendercheck = true; 
         break; 
        default: 
         message = ">>> Sorry i didn't get your gender.\nPlease try again"; 
         System.out.println(message); 
       } 
      }                       //end of gender check 
      String feel;                    //checking how the user's day is going 
      boolean feelings = false; 
      do { 
       feel = userinput.nextLine(); 
       if (feel.contains("good") || feel.contains("fine") || feel.contains("great") || feel.contains("excellent") 
         || feel.contains("brilliant") || feel.contains("wonderful")) {    //response for when the user is fine 
        System.out.print(">>> "); 
        System.out.println("I am glad to hear that!"); 
        feelings = false; 
        break; 
       } else if (feel.contains("bad") || feel.contains("horrible") || feel.contains("not good") || feel.contains("terrible") 
         || feel.contains("not so good") || feel.contains("not fine")) { 
        System.out.print(">>> "); 
        System.out.println("Everything's going to be fine. Cheer up!");     //response for when the user is not fine 
        feelings = false; 
        break; 
       } else { 
        System.out.print(">>> "); 
        System.out.println("I didnt get that.");          //response for when the user's feelings are unrecognisable 
       } 
      } while (feelings != true); 

      System.out.println(">>> Now we have moved to Free Chat"); 
      do { 
       System.out.print(">>> "); 
       String freeinput = userinput.nextLine(); 
       String[] hiRes = {"Hello!", "Hi", "Greetings", "Pleasure to talk to you", "Yo", "Hola", "What's up"}; 
       String[] quoteRes = {"I attribute my success to this: I never gave or took any excuse.\n\t –Florence Nightingale", 
        "Failure is the condiment that gives success its flavor.\n\t – Truman Capote", 
        "The most common way people give up their power is by thinking they don’t have any.\n\t –Alice Walker", 
        "Your time is limited, so don’t waste it living someone else’s life.\n\t –Steve Jobs", 
        "Believe you can and you’re halfway there.\n\t –Theodore Roosevelt", 
        "I have been impressed with the urgency of doing. Knowing is not enough; we must apply. Being willing is not enough; we must do.\n\t –Leonardo da Vinci", 
        "In order to succeed, your desire for success should be greater than your fear of failure.\n\t –Bill Cosby", 
        "You become what you believe.\n\t –Oprah Winfrey"}; 
       String[] unknownRes = {"I don't understand what you are trying to say.", "Please try again.", "ERROR 404 unknown input"}; 
       String[] byeRes = {"Aww.. Don't leave.", "See you again.", "GoodBye.", "Thank you! Bye.", "See you next time!", "Takecare.", "Have a good day.", "Farewell."}; 

       for (int a = 0; a < hiRes.length; a++) { 
        if (freeinput.replaceAll("[^a-zA-Z ]", "").equalsIgnoreCase("hi") || freeinput.replaceAll("[^a-zA-Z ]", "").equalsIgnoreCase("hey")) { 
         int rdm = new Random().nextInt(hiRes.length); 
         System.out.println(">>> " + hiRes[rdm]); 
         break; 
        } else if (freeinput.contains("quote")) { 
         int rdm = new Random().nextInt(quoteRes.length); 
         System.out.println(">>> " + quoteRes[rdm]); 
         break; 
        } else if (freeinput.contains("bye")) { 
         int rdm = new Random().nextInt(byeRes.length); 
         System.out.println(">>> " + byeRes[rdm]); 
         break; 
        } else { 
         int rdm; 
         rdm = new Random().nextInt(unknownRes.length); 
         System.out.println(">>> " + unknownRes[rdm]); 
         break; 
        } 
       } 
      } while (!userinput.equals("bye")); 

      break; 

     //main else               //printing this if the user wants to use the WordGame Bot 
     case 2: 
      System.out.print(">>> "); 
      System.out.println("Hello! I am a Word Game Bot. You can call me \"Tam\""); 
      String original = ""; 
      String wordToGuess = ""; 
      String myinput = ""; 
      boolean game = true; 
      while (game == true) { 
       Scramble game1 = new Scramble(wordToGuess); 
       do { 
       System.out.println("Your word is " + game1.scrambledWord(original)); 
       System.out.print("--- "); 
       myinput = userinput.nextLine(); 

       if (myinput.equalsIgnoreCase(game1.original())) { 
        System.out.println(">>> Great Job getting the asnwer"); 
        game = true; 
       } else if (myinput.equalsIgnoreCase("end")) { 
        System.out.println("Please come again."); 
        System.exit(0); 
       } else if (myinput.equalsIgnoreCase("answer")) { 
        System.out.println(">>> The answer is " + game1.original()); 
       } else { 
        System.out.print(">>> "); 
        System.out.println("Incorrect, please try again!"); 
        game = false; 
       } 
       }while(game == false); 
      } 

     default: 

      do { 
       System.out.print("Question: "); 
       input = userinput.nextLine(); 
       userInputQns(input); 
       System.out.print("Answers: "); 
       input = userinput.nextLine(); 
       userInputAns(input); 
      } while (!input.equalsIgnoreCase("cancel")); 

      input = userinput.nextLine(); 
      if (questions.contains(input)) { 
       System.out.println(answers.get(index)); 
      } 
    } 

}//end of main method 

顺便说一句,这是我抢班

private String[] Qnword = {"animal", "duck", "dinosaur", "butterfly", "dog", "horse", "cow", "cat", 
     "elephant", "crocodile", "alligator", "squirrel", "fox", "raccoon", "python", "spider", "amphibians", 
     "reptiles", "mammals", "scorpion", "octopus", "worm", "jellyfish", "kangaroo", "koala", "mosquito", "camel", 
     "lion", "jaguar", "cheetah", "leopard", "gorilla", "dolphin", "whale", "shark", "ladybug", "cockroach", "rooster", 
     "parrot", "snake", "hippopotamus", "rhinoceros", "coyote", "platypus", "owl", "skunk", "giraffe", 
     "wolf", "goat", "ferret", "armadillo", "donkey", "antelope", "eagle"}; 
private String original = null; 
private String scrambled = null; 
private int initial = new Random().nextInt(Qnword.length); 

public Scramble(String originalWord) { 
    this.original = originalWord; 
    this.scrambled = scrambledWord(originalWord); 
} 

public String original() {   
    return Qnword[initial]; 
} 

public String scrambledWord(String original) { 

    original = Qnword[initial]; 

    char[] charArr = original.toCharArray(); //every letter is assigned to charArr 

    for (int a = 0; a < charArr.length; a++) { 
     //generating a random number from the length of word to guess. Example: Animal, generated no = "1" 
     int rand = new Random().nextInt(charArr.length); 
     //shuffling the words 
     char temp = charArr[a];  //charArr[0] is the letter "A" which is moved to temp 
     charArr[a] = charArr[rand]; //A is then moved to charArr[rdm] which is index 1, so the word is currently "AAimals' 
     charArr[rand] = temp;  //charArr[1] contains n which is then moved to temmp, which is 0, becoming nAimal 
    } 
    return new String(charArr); 
} 
+0

请去看[问]。之后,编辑你的问题,并给它一个正确的标题... – CBroe

+0

这是不是很清楚你问的。你能否详细说明,也许用更简单的例子来演示?您所展示的大部分代码似乎与您所描述的内容完全无关。 – David

+0

如果我理解得很好,并且想要让用户再次选择菜单中的其他选项,只需添加一个while循环(具有测试菜单选项的条件,例如直到用户输入退出选项)包含你的“菜单”和你的开关盒。 – Maaaatt

回答

1

对于您必须添加你的整个代码do while循环,并给予一定的条件退出循环。

do{ 
    System.out.println("Press \"1\" to chat" + " & " + "\"2\" to play games" + " & \"3\" to edit the conversations"); 

     int startup = choice.nextInt(); 
     String returningCode = returnChoiceOfChatbot(startup); 
     System.out.println(returningCode); 

     switch (startup) { 
      //end of main if 
      case 1: 
       //if the user wants the chatbot 
       System.out.println("Bot:\tI am Mat, a chatbot. May i know your name please?");   //asking for the user's name 
       String name = userinput.nextLine(); 
       name = name.toLowerCase(); 
       String username = name.substring(0, 1).toUpperCase() + name.substring(1); 
       message = ">>> \tHello! " + username + "! Are you a boy or a girl?";      // asking for gender 
       System.out.println(message); 
       while (gendercheck != true) { 
        GenderInput = userinput.nextLine().toLowerCase();// F 
        switch (GenderInput) { 
         case "boy": 
         case "man": 
         case "m": 
         case "male": 
         case "dude": 
         case "guy": 
          System.out.println(">>> \tHi. Mr. " + username + ". How are you?");    //printing out Mr and name if it is a male 
          gendercheck = true; 
          break; 
         case "girl": 
         case "female": 
         case "f": 
         case "woman": 
         case "lady": 
          System.out.println(">>> \tHi. " + "Ms. " + username + ". How are you?");  //printing out Ms and name if it is a female 
          gendercheck = true; 
          break; 
         default: 
          message = ">>> Sorry i didn't get your gender.\nPlease try again"; 
          System.out.println(message); 
        } 
       }                       //end of gender check 
       String feel;                    //checking how the user's day is going 
       boolean feelings = false; 
       do { 
        feel = userinput.nextLine(); 
        if (feel.contains("good") || feel.contains("fine") || feel.contains("great") || feel.contains("excellent") 
          || feel.contains("brilliant") || feel.contains("wonderful")) {    //response for when the user is fine 
         System.out.print(">>> "); 
         System.out.println("I am glad to hear that!"); 
         feelings = false; 
         break; 
        } else if (feel.contains("bad") || feel.contains("horrible") || feel.contains("not good") || feel.contains("terrible") 
          || feel.contains("not so good") || feel.contains("not fine")) { 
         System.out.print(">>> "); 
         System.out.println("Everything's going to be fine. Cheer up!");     //response for when the user is not fine 
         feelings = false; 
         break; 
        } else { 
         System.out.print(">>> "); 
         System.out.println("I didnt get that.");          //response for when the user's feelings are unrecognisable 
        } 
       } while (feelings != true); 

       System.out.println(">>> Now we have moved to Free Chat"); 
       do { 
        System.out.print(">>> "); 
        String freeinput = userinput.nextLine(); 
        String[] hiRes = {"Hello!", "Hi", "Greetings", "Pleasure to talk to you", "Yo", "Hola", "What's up"}; 
        String[] quoteRes = {"I attribute my success to this: I never gave or took any excuse.\n\t –Florence Nightingale", 
         "Failure is the condiment that gives success its flavor.\n\t – Truman Capote", 
         "The most common way people give up their power is by thinking they don’t have any.\n\t –Alice Walker", 
         "Your time is limited, so don’t waste it living someone else’s life.\n\t –Steve Jobs", 
         "Believe you can and you’re halfway there.\n\t –Theodore Roosevelt", 
         "I have been impressed with the urgency of doing. Knowing is not enough; we must apply. Being willing is not enough; we must do.\n\t –Leonardo da Vinci", 
         "In order to succeed, your desire for success should be greater than your fear of failure.\n\t –Bill Cosby", 
         "You become what you believe.\n\t –Oprah Winfrey"}; 
        String[] unknownRes = {"I don't understand what you are trying to say.", "Please try again.", "ERROR 404 unknown input"}; 
        String[] byeRes = {"Aww.. Don't leave.", "See you again.", "GoodBye.", "Thank you! Bye.", "See you next time!", "Takecare.", "Have a good day.", "Farewell."}; 

        for (int a = 0; a < hiRes.length; a++) { 
         if (freeinput.replaceAll("[^a-zA-Z ]", "").equalsIgnoreCase("hi") || freeinput.replaceAll("[^a-zA-Z ]", "").equalsIgnoreCase("hey")) { 
          int rdm = new Random().nextInt(hiRes.length); 
          System.out.println(">>> " + hiRes[rdm]); 
          break; 
         } else if (freeinput.contains("quote")) { 
          int rdm = new Random().nextInt(quoteRes.length); 
          System.out.println(">>> " + quoteRes[rdm]); 
          break; 
         } else if (freeinput.contains("bye")) { 
          int rdm = new Random().nextInt(byeRes.length); 
          System.out.println(">>> " + byeRes[rdm]); 
          break; 
         } else { 
          int rdm; 
          rdm = new Random().nextInt(unknownRes.length); 
          System.out.println(">>> " + unknownRes[rdm]); 
          break; 
         } 
        } 
       } while (!userinput.equals("bye")); 

       break; 

      //main else               //printing this if the user wants to use the WordGame Bot 
      case 2: 
       System.out.print(">>> "); 
       System.out.println("Hello! I am a Word Game Bot. You can call me \"Tam\""); 
       String original = ""; 
       String wordToGuess = ""; 
       String myinput = ""; 
       boolean game = true; 
       while (game == true) { 
        Scramble game1 = new Scramble(wordToGuess); 
        do { 
        System.out.println("Your word is " + game1.scrambledWord(original)); 
        System.out.print("--- "); 
        myinput = userinput.nextLine(); 

        if (myinput.equalsIgnoreCase(game1.original())) { 
         System.out.println(">>> Great Job getting the asnwer"); 
         game = true; 
        } else if (myinput.equalsIgnoreCase("end")) { 
         System.out.println("Please come again."); 
         System.exit(0); 
        } else if (myinput.equalsIgnoreCase("answer")) { 
         System.out.println(">>> The answer is " + game1.original()); 
        } else { 
         System.out.print(">>> "); 
         System.out.println("Incorrect, please try again!"); 
         game = false; 
        } 
        }while(game == false); 
       } 

      default: 

       do { 
        System.out.print("Question: "); 
        input = userinput.nextLine(); 
        userInputQns(input); 
        System.out.print("Answers: "); 
        input = userinput.nextLine(); 
        userInputAns(input); 
       } while (!input.equalsIgnoreCase("cancel")); 

       input = userinput.nextLine(); 
       if (questions.contains(input)) { 
        System.out.println(answers.get(index)); 
       } 
     } 

    } 
}while(some condition); 
//end of main method 

希望它能帮上忙。

相关问题