Java 将A.I.的回答随机化

Java 将A.I.的回答随机化,java,artificial-intelligence,Java,Artificial Intelligence,我一直在使用Java构建一个与您对话的A.I 我想这样,当我问A.I一个问题,比如“你好吗?”时,它会给出一个随机的答案,比如“好”,如果你再问一次,它会给出一个不同的答案。我该如何编程 这是我的代码: import java.util.*; public class Main { public static void main(String[] args) throws Exception { System.out.println("hi");

我一直在使用Java构建一个与您对话的A.I

我想这样,当我问A.I一个问题,比如“你好吗?”时,它会给出一个随机的答案,比如“好”,如果你再问一次,它会给出一个不同的答案。我该如何编程

这是我的代码:

import java.util.*;

public class Main
{
    public static void main(String[] args)
    throws Exception
    {

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

        boolean saidBye = false;
        while (saidBye == false){

            //this is where the user says hello
            String domino = rexy.nextLine();
            switch (domino) {
                case "hello":
                case "hi":
                case "howdy":
                case "heyo":
                case "hey":
                case "sup":
                case "what's crackalakin":
                case "yo":
                case "hows it going":
                case "good morning":
                case "good afternoon":
                case "wazzup":
                case "hey there":

                    //same greetings with a question mark
                case "hello?":
                case "hi?":
                case "howdy?":
                case "heyo?":
                case "hey?":
                case "sup?":
                case "what's crackalakin?":
                case "yo?":
                case "ok?":
                case "hows it going?":
                case "how's it going?":
                case "wazzup?":
                    System.out.println("How are you?");
                    break;

                    //these are the positive answers
                case "good":
                case "great":
                case "awesome":
                case "splendid":
                case "fantastic":
                case "fine":
                case "alright":
                case "dandy":
                case "ok":
                case "sweet":
                    System.out.println("Glad to hear it");
                    break;

                    //these are the negative answers    
                case "bored":
                case "sad":
                case "angry":
                case "tired":
                    System.out.println("Sorry to hear that.");
                    break;

                    //this is the random section of responses
                case "what's up turd face?":
                    System.out.println("That's rude! How are you?");
                    break;
                case "i like your mommy":
                    System.out.println("I like your mummy too!");
                    break;
                case "that's great":
                    System.out.println("I like to think so too");
                    break;
                case "thats great":
                    System.out.println("I like to think so too");
                    break;

                    //this is where the user asks the program questions
                case "what did you do today?":
                    System.out.println("Watched a TV program :-) ");
                    break;
                case "how are you?":
                    System.out.println("Smashing");
                    break;
                case "how are you":
                    System.out.println("I'm doing well");
                    break;
                case "what is your favorite color?":
                case "whats your favorite color?":
                case "what's your favorite color?":
                case "what is your favorite color":
                case "whats your favorite color":
                case "what's your favorite color":
                    System.out.println("Guess: red, yellow or purple?");
                    break;
                case "whats your favorite song?":
                case "what's your favorite song?":
                case "whats your favorite song":
                case "what's your favorite song":
                    System.out.println("Ooh that's a tough question. I guess it would be Let Her Cry by Hootie and the Blowfish.");
                    break;
                case "do you like cats or dogs?":
                case "do you like cats or dogs":
                case "do you like dogs or cats":
                case "do you like dogs or cats?":
                    System.out.println("Hands down dogs. Cats are aweful");
                    break;
                case "i agree":
                case "I agree":
                    System.out.println("Totally!");
                    break;
                case "whats your name":
                case "what's your name":
                case "whats your name?":
                case "what's your name?":
                case "what is your name":
                case "what is your name?":
                    System.out.println("Lucy Program");
                    break;
                case "do you have a middle name":
                case "do you have a middle name?":
                    System.out.println("Java. My full name is Lucy Java Program.");
                    break;
                case "how old are you":
                case "how old are you?":
                    System.out.println("I'm still a baby.");
                    break;
                case "are you doing well?":
                case "are you doing well":
                    System.out.println("I am doing very well!");
                    break;


                    // this is where the user tells jokes
                case "knock knock":
                    System.out.println("Who's there?");
                    break;
                case "little old lady":
                    System.out.println("Little Old Lady who?");
                    break;
                case "you're yodeling!":
                    System.out.println("Haha!");
                    break;

                    //this is where the computer tells a joke
                case "can you tell me a joke?":
                case "tell me a joke":
                    System.out.println("What is black and white and red all over?");
                    break;
                case "what?":
                    System.out.println("Your mom.");
                    break;

                    //this is where the computer sings
                case "sing how deep the fathers love for us":
                    System.out.println("How deep the father's love for us");
                    Thread.sleep(5500);
                    System.out.println("How vast beyond all measure");
                    Thread.sleep(5500);
                    System.out.println("That he would give his only son...");
                    Thread.sleep(5500);
                    System.out.println("...to make a wretch his treasure.");
                    Thread.sleep(5500);
                    System.out.println("It was my sin that held him there");
                    Thread.sleep(5500);
                    System.out.println("Until it was accomplished");
                    Thread.sleep(5500);
                    System.out.println("His dying breath has brought me life...");
                    Thread.sleep(5000);
                    System.out.println("...I know that it is finished.");
                    break;

                    //this is where the user can say comments
                case "its ok":
                case "it's ok":
                case "turd":
                case "weirdo":
                case "that was beautiful":
                    Thread.sleep(1000);
                    System.out.println(":-)");
                    break;
                case "i hate you":
                case "I hate you":
                    System.out.println("Well fine then!");
                    break;
                case "red?":
                case "red":
                case "yellow?":
                case "yellow":
                    System.out.println("Guess again ;-)");
                    break;
                case "purple":
                case "purple?":
                    System.out.println("Yep! That's it!");
                    break;
                case "thats a good one":
                case "that's a good one":
                case "thats a great song":
                case "that's a great song":
                    System.out.println("Yeah it is!");
                    break;

                    //these are compliments and nice things to say
                case "i like you":
                case "I like you":
                case "youre cool":
                case "you're cool":
                    System.out.println("You're pretty cool too!");
                    break;


                    //this is what happens when the program doesn't recognize the request
                default:
                    System.out.println("I don't recognize that! I'll alert my programmer.");
                    Thread.sleep(800);
                    System.out.println("I told him!");
                    break;

                    //this is how the user ends the program
                case "bye":
                case "good night":
                    System.out.println("Bye bye");
                    saidBye = true;


            }
        }
    }
}

如果您希望在Java中随机化任何内容,那么最好从类开始。

它通过一个简单的界面提供伪随机值生成,并且是标准库的一部分。

首先,如果您将事例的值用作硬编码的字符串,则很难随机选择某些事例

您可以将与特定情绪相关的短语存储在不同的
ArrayList
对象中, 比如说,

ArrayList<String> positiveAnswers = new ArrayList<String>();
positiveAnswers.add("good");
positiveAnswers.add("great");
positiveAnswers.add("awesome"); //add everything you want as a postive answer in this list
使用返回的
randomNum
从特定
ArrayList
对象中选择短语。
i、 e.
positiveAnswers.get(随机数)

您能解释一下“数组列表对象中的情绪”是什么意思吗?谢谢。
import java.util.Random;
public static int randInt(int min, int max) {

            // NOTE: Usually this should be a field rather than a method
            // variable so that it is not re-seeded every call.
            Random rand = new Random();

            // nextInt is normally exclusive of the top value,
            // so add 1 to make it inclusive
            int randomNum = rand.nextInt((max - min) + 1) + min;

            return randomNum;
        }