Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么我需要输入我的选择两次?_Java_Input_Selection - Fatal编程技术网

Java 为什么我需要输入我的选择两次?

Java 为什么我需要输入我的选择两次?,java,input,selection,Java,Input,Selection,有人能帮我理解为什么程序要求我在继续之前输入两次选择吗 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.prin

有人能帮我理解为什么程序要求我在继续之前输入两次选择吗

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语句,但我似乎无法解决这个问题。任何提示都将不胜感激

这是因为你需要

String initialSelection = scan.next();


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

,这很有意义。非常感谢!
        boolean stringInput = scan.hasNext();
        boolean intInput = scan.hasNextInt();
        boolean boolinput = scan.hasNextBoolean();