简体   繁体   English

允许在Java中进行多个用户输入

[英]allowing multiple user inputs in java

I am making an AI that carries on a conversation with you. 我正在制作与您进行对话的AI。 when you run the program, the computer says hi, and the user can enter multiple greetings back (like you, howdy etc.) 当您运行该程序时,计算机打招呼,并且用户可以输入多个问候语(例如您,howdy等)。

my question is when the computer asks the user "How are you?" 我的问题是计算机问用户“你好吗?” and the user answers. 然后用户回答。 I programmed a switch where if you say something like "good" the computer will reply "Glad to hear it." 我编写了一个开关,如果您说“好”之类的话,计算机将回答“很高兴听到”。 , but it doesn't. ,但事实并非如此。 what did I Do wrong? 我做错了什么?

here is my code: 这是我的代码:

    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);
    String domino = rexy.nextLine();


    switch (domino){
        case "hello":
            System.out.println("How are you?");
            break;

        case "hi":
            System.out.println("How are you?");
            break;

        case "howdy":
            System.out.println("How are you?");
            break;

        case "heyo":
            System.out.println("How are you?");
            break;

        case "hello?":
            System.out.println("How are you?");
            break;

        case "hey":
            System.out.println("How are you?");
            break;

        case "sup":
            System.out.println("How are you?");
            break;

        case "good":
            System.out.println("Glad to hear it");
            break;

        case "great":
            System.out.println("Glad to hear it");
            break;

        case "awesome":
            System.out.println("Glad to hear it");
            break;

        case "splendid":
            System.out.println("Glad to hear it");
            break;

        case "fantastic":
            System.out.println("Glad to hear it");
            break;


        case "fine":
            System.out.println("Glad to hear it");
            break;

        case "what's crackalakin?":
            System.out.println("How are you?");
            break;

        case "what's up turd face?":
            System.out.println("That's rude! How are you?");
            break;

    }
}

} }

thanks. 谢谢。

You could try adding a default statement to the switch, so that there is fallback for when the answer is not recognized; 您可以尝试向交换机添加default语句,以便在无法识别答案时进行回退。 like that: 像那样:

switch (domino) {
  //...
default:
    System.out.println("Sorry, I don't understand that.");
    break;
}

Furthermore, you can try printing the domino String, to see what is actually being read. 此外,您可以尝试打印domino字符串,以查看实际读取的内容。

System.out.println(domino);

Also, a tip: you can join multiple equal case statements in a switch like so: 另外,提示:您可以像这样在switch连接多个相等case语句:

switch (domino) {
    case "hello":
    case "hi":
    case "howdy":
    case "heyo":
    case "hello?":
    case "hey":
    case "sup":
        System.out.println("How are you?");
        break;
    case "good":
    case "great":
    case "awesome":
    case "splendid":
    case "fantastic":
    case "fine":
        System.out.println("Glad to hear it");
        break;
}

If you really would like to do this simple but not really elegant - you can put the readLine and app answers in some kind of loop like below: 如果您确实想做到这一点,但又不是很优雅-您可以将readLine和应用程序答案放入如下所示的某种循环中:

public class Test {

    public static void main(String args[]) throws InterruptedException {
        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) {
            String domino = rexy.nextLine();
            switch (domino) {
                case "hello":
                case "hi":
                case "howdy":
                case "heyo":
                case "hello?":
                case "hey":
                case "sup":
                case "what's crackalakin?":
                    System.out.println("How are you?");
                    break;
                case "good":
                case "great":
                case "awesome":
                case "splendid":
                case "fantastic":
                case "fine":
                    System.out.println("Glad to hear it");
                    break;
                case "what's up turd face?":
                    System.out.println("That's rude! How are you?");
                    break;
                case "bye":
                    System.out.println("Bye bye");
                    saidBye = true;
                    break;
            }
        }
    }
}

More elegant way would be to put every part of the conversation into separate method like that: 更为优雅的方法是将对话的每个部分放入类似的单独方法中:

public class Android {

    private Scanner rexy;

    public static void main(String args[]) throws InterruptedException {

        Scanner rexy = new Scanner(System.in);
        Android android = new Android(rexy);
        android.greet();
        android.beNice();
        android.sayBye();
    }

    public Android(final Scanner rexy) {
        this.rexy = rexy;
    }

    public void greet() {

        System.out.println("hi");

        System.out.println("The computer would like to remind you to reply with only a greeting");

        String domino = rexy.nextLine();
        switch (domino) {
            case "hello":
            case "hi":
            case "howdy":
            case "heyo":
            case "hello?":
            case "hey":
            case "sup":
            case "what's crackalakin?":
                break;
            default:
                System.out.print("That's rude! ");
                break;
        }
    }

    private void beNice() {
        System.out.println("How are you?");
        String domino = rexy.nextLine();

        switch (domino) {
            case "good":
            case "great":
            case "awesome":
            case "splendid":
            case "fantastic":
            case "fine":
                System.out.println("Glad to hear it");
                break;
        }
    }

    private void sayBye() {
        System.out.println("OK. I have to go now.");
        String domino = rexy.nextLine();

        switch (domino) {
            case "bye":
            case "cu":
            case "see you":
                System.out.println("Bye");
                break;
        }
    } 
}

But it still has a lot of room for improvement ;) 但是它仍有很大的改进空间;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM