-1

プログラムが続行する前に選択を2回入力する必要がある理由を誰かが理解するのを手伝ってもらえますか?

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();

両方ある場合は2回入力する必要があります

于 2013-02-01T20:53:17.027 に答える