-1

谁能帮我理解为什么程序要求我在继续之前输入我的选择两次?

public static void main(String[] args) {
    String quit = "quit";
    String input =  "input";
    String print = "print";


    @SuppressWarnings("resource")
    Scanner scan = new Scanner(System.in);
    System.out.println("please enter a command : \n      1) Quit \n      2) Input \n      3) Print \n");

    String initialSelection = scan.next();
    boolean stringInput = scan.hasNext();
    boolean intInput = scan.hasNextInt();
    boolean boolinput = scan.hasNextBoolean();

    if(initialSelection.equalsIgnoreCase(quit)) {
        System.out.println("The program has quit.");
        System.exit(0);
    }else if(initialSelection.equalsIgnoreCase(print)){
        [constructor]

        do {
            System.out.println("\nplease enter a command : \n      1) Quit \n      2) Input \n      3) Print \n");
            initialSelection = scan.nextLine();

                if (initialSelection.equalsIgnoreCase(print)) {
                        new BonusArrayList();
                } else if(initialSelection.equalsIgnoreCase(quit)) {
                            System.out.println("The program has quit.");
                            System.exit(0);
                }
        } while (initialSelection.equalsIgnoreCase(print));

    }else if(initialSelection.equalsIgnoreCase(input)){
        System.out.println("\n Please enter some kind of value (ex: 123, hello, True)");
    }
}

我相信它来自嵌套的 do-while 语句,但我似乎无法解决这个问题。任何提示将不胜感激!

4

1 回答 1

0

这是因为你要么需要

String initialSelection = scan.next();

或者

        boolean stringInput = scan.hasNext();
        boolean intInput = scan.hasNextInt();
        boolean boolinput = scan.hasNextBoolean();

如果两者都有,则需要输入两次

于 2013-02-01T20:53:17.027 回答