-1

我正在尝试使用循环创建一个聊天机器人,该循环将与用户进行对话,并在用户键入“再见”时停止。有点像我在这里尝试做的事情,除了我不擅长编程:

    Scanner sc = new Scanner (System.in);
    String question;

    System.out.println("Hello");

    do
    {
        question = sc.nextLine();

        if (question.equals("how are you");
        {
            System.out.println("good");
        }

        if (question.equals("bye"))
        {
            System.out.println("bye");
            break;
        }
    } while (!sc.nextLine().equals("bye"));
}   

}

4

1 回答 1

1

你应该把你的“再见”从循环中解脱出来。因此,如果sc.nextLine().equals("bye")它完成循环,说“再见”并完成程序。

    Scanner sc = new Scanner (System.in);
    String question;

    System.out.println("Hello");

    do
    {
        question = sc.nextLine();

        if (question.equals("how are you");
        {
            System.out.println("good");
        }

    } while (!question.equals("bye"));    

            System.out.println("bye");
于 2014-11-29T01:00:47.297 回答