在java中允许多个用户输入

时间:2014-08-18 22:21:37

标签: java input

我正在和你进行对话。 当你运行程序时,计算机会打个招呼,用户可以输回多个问候语(像你,你好等等)。

我的问题是当计算机询问用户时#34;你好吗?"并且用户回答。我编写了一个开关,如果你说" good"电脑会回复#34;很高兴听到它。" ,但它没有。我做错了什么?

这是我的代码:

    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;

    }
}

}

感谢。

2 个答案:

答案 0 :(得分:2)

您可以尝试向交换机添加default语句,以便在无法识别答案时存在回退;那样:

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

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

System.out.println(domino);

另外,提示:您可以在case中加入多个相同的switch语句,如下所示:

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

答案 1 :(得分:0)

如果你真的想做这个简单但不是很优雅 - 你可以将readLine和app答案放在某种循环中,如下所示:

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

更优雅的方式是将对话的每个部分都放在单独的方法中:

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

但它仍有很大的改进空间;)