java - java中允许多个用户输入

标签 java input

我正在制作一个可以与你进行对话的人工智能。 当你运行程序时,计算机会说“嗨”,用户可以输入多个问候语(例如“你”、“你好”等)

我的问题是当计算机询问用户“你好吗?”时和用户回答。我编写了一个开关,如果你说“好”之类的话,计算机就会回复“很高兴听到它”。 ,但事实并非如此。我做错了什么?

这是我的代码:

    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;

    }
}

}

谢谢。

最佳答案

您可以尝试在开关中添加一个default语句,以便在无法识别答案时有后备;像这样:

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

此外,您可以尝试打印 domino 字符串,以查看实际读取的内容。

System.out.println(domino);

另外,还有一个提示:您可以在 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;
}

关于java - java中允许多个用户输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25372949/

相关文章:

java - FXML如何去除边框

ruby-on-rails-3 - Rails 如何翻译表单输入 file_field ( i18n ) 上的标题

javascript - 输入数字禁用在 Firefox 上不起作用

c++ - 文件输入和指针所有权语义

java - ProcessBuilder 找不到命令,尽管它可以在终端中找到

Java检查数据库中是否存在该值

java - 如何在调试 View 中过滤动态生成的类?

javascript - val() 函数给出控制台错误

c - 如何检查它是否是 C 中的输入 EOF(ctrl + D)

java - 在Android应用程序中定义位图的大小