首页 > 解决方案 > Java初学者,input.close(); 在做while循环之后,如何?

问题描述

我完全是初学者,无法弄清楚这里的问题 - 我希望在用户输入“退出”后关闭扫描仪输入。当我尝试我的代码时,它显示“退出>”,我仍然可以输入。是否与 input.close() 的范围有关?

    String command;     
    do {
        System.out.print("> ");
        command = input.nextLine();


        switch (command) {
        case "register new dog":
            System.out.println("You gave command: register new dog");
            System.out.println("Command? ");
            break;

        case "increase age":
            System.out.println("You gave command: increase age");
            System.out.println("Command? ");
            break;

        case "list dogs":
            System.out.println("You gave command: list dogs");
            System.out.println("Command? ");
            break;

        case "remove dogs":
            System.out.println("You gave command: remove dogs");
            System.out.println("Command? ");
            break;

        case "exit":
            System.out.println("Goodbye!");
            break;

        default:
            System.out.println("Error: invalid command, choose again: ");
            System.out.println("Register new dog");
            System.out.println("Increase age");
            System.out.println("List dogs");
            System.out.println("Remove dogs");
            System.out.println("Exit");



        }

    } while (command != "exit");

    input.close(); 

标签: javajava.util.scanner

解决方案


推荐阅读